3

Like ng-enabled, ng-src, ng-class There is something generic that make it work for any attribte (ng-x)?

For example, this is my code:

<div ng-attrbuite1="a" ng-otherattribute="b"></div> 

I want it to set:

<div attribute1="a_value" attribute2="b_value_in_scope"> 

In runtime I don't know yet, the names of the attribute that will be exists in the HTML.

4
  • I suggest you read about DIRECTIVES, this is a good article about the subject: undefinednull.com/2014/02/11/… Commented Jan 24, 2016 at 0:09
  • I recommend that you don't name your directives ng-*. See the 2nd best practice block docs.angularjs.org/guide/directive#creating-directives Commented Jan 24, 2016 at 0:11
  • no...there is nothing implicit in ng- unless the core directive actually exists Commented Jan 24, 2016 at 0:34
  • I know what is directives. Searching for a smart shortcut if it exists Commented Jan 24, 2016 at 9:17

2 Answers 2

2

You can use directives. Here is a simple example:

HTML

<div ng-app="myApp"> <div ui-foo="foo">foo scope variable: {{foo}}</div> </div> 

JS

angular.module('myApp', []).directive('uiFoo', function() { return { restrict: 'EAC', link: function($scope, element, attrs, controller) { $scope.foo = attrs.uiFoo; } }; } ); 
Sign up to request clarification or add additional context in comments.

Comments

1

If you are meaning you want to have a catch-all where all the attributes prefixed with say my (as you should use your own prefix) get evaluated and then set to their respective non-prefixed attribute you will need to:

  1. Make your own custom directive
  2. Find all attributes on the element (provided by $attrs)
  3. Get the evaluated value
  4. Set the actual attribute on the element

I won't cover making the actual directive you can find out that here

In the link function of your directive one of the arguments will be $attrs. $attrscontains the normalized(camel cased) and non-normalized attribute names, their respective evaluated values(if using expressions), and some helper methods.

So you can simply loop over the $attrs (or $attrs.$attr) keys, filtering out ones you shouldn't be using like ng-model, ng-selected, etc, get the evaluated value and then set the respective non-prefixed counterpart.

Object.keys($attrs).filter(function(key){ //filter out keys like ngModel,ngSelect,$set etc return /^my/.test(key); }).forEach(function(key){ //$attrs.$attr contains the non-normalized //versions of the attributes //this example assumes you will prefix with `my` //so remove `my-` to get the attribute name to set var attrName = $attrs.$attr[key].replace("my-",""); //Use $parse() if you just want to use some scope's property name //for example my-attribute="somePropertyName" $attrs.$set(attrName,$parse($attrs[key])($scope)); //Use just $attrs[key] if you are using some expression //for example my-attribute="{{somePropertyName}}" $attrs.$set(attrName,$attrs[key]); }); 

angular.module("test",[]) .controller("ctrl",function($scope){ $scope.someprop = "Some Value"; }) .directive("caAttr",function($parse){ return { restrict : 'A', link : function($scope, $element, $attrs) { console.log($attrs); Object.keys($attrs).filter(function(key){ return /^my/.test(key); }).forEach(function(key){ var attrName = $attrs.$attr[key].replace("my-",""); $attrs.$set(attrName,$parse($attrs[key])($scope)); }); angular.element("#log").text( angular.element("#container").html() ); } }; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="test" ng-controller="ctrl"> <div id="container"> <div ng-model="mymodel" ng-selected="{{nothing}}" my-stack="someprop" ca-attr></div> </div> <div id="log"></div> </div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.