trying to access controller variable in ng-repeat with same name. e.g.
Controller
$scope.items = [1,2,3,4,5];
$scope.a = {
data: "Some Data"
};
View
<div ng-repeat="a in items"> {{ a }} - {{ $parent.a.data }} </div>
but it is not working. is it possible to access that variable (Object) inside ng-repeat with same name?
One way to make it, is accessing variables by naming your controller (Some example here in doc).
You can name your controller like ng-controller="TestCtrl as ctrl"
Then, you need some changes in your view :
<div ng-repeat="a in ctrl.items"> {{ a }} - {{ ctrl.a.data }} </div>
And in your controller :
app.controller('TestCtrl', function($scope) {
var self = this;
self.items = [1,2,3,4,5]; //brackets here, not curly brackets as said by @georgeawg
self.a = {
data: "Some Data"
};
// Rest of you code
});
Finally, naming controllers is quite a good practice.
Hope it helps !
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments