I'm attempting to utilize $parse
to analyze the expression of an attribute of a directive within an ng-repeat loop.
Here is an example of the HTML:
<body ng-app="hello" ng-controller="control">
<div ng-repeat="a in array" my-dir="a.one == 1"></div>
</body>
and the Angular directive:
angular.module('hello', [])
.controller('control', function($scope){
$scope.array = [{
one: 1,
two: 2
},
{
one: 3,
two: 4
}];
})
.directive('myDir', function($parse){
return {
restrict: 'A',
link: function(scope, elem, attrs){
var hash = $parse(attrs.myDiv);
// I am seeking a method to evaluate this expression
// without needing to define it as my-dir="array[$index].item == 1"
// similar to what is done with other Angular directives.
console.log(hash(scope)); // This returns undefined, as a.one cannot be found in the scope.
},
};
});
The problem with this approach is that $parse
is attempting to locate the a.one
property within the scope
and that property does not exist.
I could use my-dir="array[$index].one == 1"
but this is not user-friendly for the users of the directive (they would need to know the scope they are working with).