I have the following code:
/>
Copy This will essentially run any code you need, and doesn't involve you having to parse out data attributes from the DOM.
You could write a pretty trivial directive to pull in the value and publish using an expression. This will essentially accomplish the same thing, but is more difficult in my opinion:
angular.module ('data-pluck' , [])
.controller ('fooController' , function ( ) {
this .name = 'Foo Controller' ;
})
.directive ('pluckData' , ['$parse' ,
function ($parse ) {
return {
restrict : 'A' ,
link : function (scope, elem, attrs ) {
var expression = function ( ) {};
expression.assign = function ( ) {};
scope.$watch(attrs.placeData , function ( ) {
expression = $parse(attrs.placeData );
});
scope.$watch(attrs.pluckData , function ( ) {
expression.assign (scope, attrs[attrs.pluckData ]);
});
}
};
}
]);
Copy <script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js" > </script > <div ng-app ='data-pluck' ng-controller ='fooController as ctrl' > <h1 > {{ctrl.name}}</h1 > <div data-my-val ="I'm value one" pluck-data ='myVal' place-data ='ctrl.valueOne' > <p > I'm a regular old <code > < p> </code > tag</p > <input type ='hidden' data-my-val ="I'm the second value" pluck-data ='myVal' place-data ='ctrl.valueTwo' /> </div > <h3 > These Values Populated Dynamically</h3 > <ul > <li > ctrl.valueOne = {{ctrl.valueOne}}</li > <li > ctrl.valueTwo = {{ctrl.valueTwo}}</li > </ul > </div >
Copy Solution 2:
Angular comes with jqLite built in, which still has the attr() function. But it's not the Angular "way" to be manually fiddling around in the DOM from a controller. Your scope should be the interface between them.
I'm curious as to why you have a value in an attribute in your UI that isn't defined first in your model / scope? How does this value get changed? Is there a reason why you can't set it in the controller:
$scope .start = 2;
Copy and then:
<input data -ng-model="typeId" name="typeId" type="hidden" data -start="{{start}}" />
Copy Can you explain a little about what data-start is meant to do?
Post a Comment for "How To Get An Element's Attribute With Angularjs"