2

Lets say we have an AngularJS view whith a master detail relation. e.g. order and order details.

And we assume the details are rendered using an ng-repeat over order.details. e.g.

<div ng-repeat="detail in orderCtl.order.details" 
     ng-controller="detailController as detailCtl">

I know that I can fetch the $scope.detail when the detailController is initialized.

 /* initcontroller */ 
 function($scope) {
      console.log($scope.detail); //<-- this is the active item
 }

But, that feels like a hack since I need to know in the controller what the ng-repeat variable name is, in this case detail

Is there any other way to get the active item from the repeater in the controller init?

If someone would change the ng-repeat variable name from detail to something else, then the controller would no longer work as expected.

1
  • Can you provide an example (in code)? The question is unclear, currently. Commented Jan 22, 2015 at 9:21

1 Answer 1

3

As I understood, you want to pass detail variable into nested scope, and you don't want to worry to remember variable name in case that you have to use nested Controller inside another ng-repeat?

You can do it very easily

<div ng-repeat="detail in orderCtl.order.details" 
 ng-controller="detailController as detailCtl" ng-init="myVarname= detail">

<div ng-repeat="historydetails in orderCtl.historyorder.details" 
 ng-controller="detailController as detailCtl" ng-init="myVarname= historydetails ">

and remember to use myVarname in detailController. Of course that is not perfect, because you have to remember that detailController needs myVarname to work.

Sign up to request clarification or add additional context in comments.

1 Comment

@Pablo De Nadai, No, i should like to thank you, I haven't even noticed that I've used English so bad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.