How do you set the scope value for something like this:
<div ng-controller="MyCtrl">
<my-element ng-repeat="p in people" person='p'></my-element>
</div>
var myApp = angular.module('myApp',[]);
myApp.directive('myElement', function(){
return {
restrict: 'E',
template: '<div>{{ name }}</div> <div>{{ age }}</div>'
}
});
function MyCtrl($scope) {
$scope.people = [{ name: 'Mike', age: 20 },{name: 'Peter', age: 22 }];
}
If by “set the scope value” you mean have the template work, then
Since each iteration of ng-repeat creates a new child scope,
pis defined on that scope. Since your directive is not creating an isolate scope, you don’t need attributeperson, so this works with the above:If you want an isolate scope, use
and