AngularJS: controller inside ng-repeat - how to access a variable
I need to access a variable from ng-repeat
inside a controller that is inside the ng-repeat of the below code. Unfortunately, this doesn't work for me.
<div ng-controller="ParentCtrl">
<div ng-repeat="r in results">
<div ng-controller="SomeCtrl">
<p>{{r.something}}</p> ($parent.r.something won't work either)
</div>
</div>
</div>
Is it possible? And if it is possible - how to do it?
EDIT: I need to do an ng-repeat inside one controller, make a list of available tables (in the parent controller, I have a search, filters, etc.), and then when I click a button on the selected row of the list (results), I need take its (inline) parameters and do something with them INSIDE the second (child?) controller.
source to share
ng-repeat must be nested in ng controller if you want to access variables in ng controller. Assuming you have "results" defined as objects in SomeCtrl, you can access it using ng-repeat. Example:
In your .js controllers
appControllers.controller('SomeCtrl', function(){
$scope.results = someData;
});
In your opinion
<div ng-controller="SomeCtrl">
<div ng-repeat="r in results">
<p>{{r.property}}</p>
</div>
</div>
source to share
Another solution would be to use the syntax as . See plunker for details .
<div ng-controller="ParentCtrl as parent">
<div ng-repeat="r in parent.results">
<div ng-controller="SomeCtrl as child">
<p>{{child.something(r)}}</p>
</div>
</div>
</div>
The recommended way to communicate through the controller is through services. But in your case, I'm not sure if creating a cross service will solve anything. What to think about though
source to share