gbos
gbos

Reputation: 570

How can I access to the ng-repeat scope?

My ng-repeat creates different elements and I need to do some operations, like creating variables, only on the ng-repeat scope.

How can I retrieve the ng-repeat scope?
How can I do something like this?

<div ng-repeat="item in items">
   <button onclick="load(item, $scope)"></button>
</div>

$scope has to be the ng-repeat scope only. Is this possible?


EDIT

that's the issue:

The code:

HTML

<div ng-repeat="item in items" class="container">  

    <div class="row">

        Name: {{item.name}}  

        <select ng-model="item['id']"
        ng-options="opt for opt in ids"
        ng-change="loadTypes(item)"
        >
            <option value="">Default</option>            
        </select>

        <select ng-model="item['type']"
        ng-options="opt for opt in types"
        >
            <option value="">Default</option>
        </select>
   </div>

</div>

Controller

//Executed one time when module is loaded
$scope.init = function(){

    //All ids on the DB        
    $scope.ids = getIdsFromServer();
}

//Executed when an Ids select changes
$scope.loadTypes = function(item){

    //Possible types for an Id
    types = getTypesFromServer(item.id);

    /*
    thisItemNgRepeatScope.types = getTypesFromServer(item.id) ?!??!! Something like this?
    */
}

The problem:

Ids it's the same in all the whole document and that's ok, it's works properly.

But I want to change the model for the second select (types) only on the ng-repeat scope. So when I change Id, I get all possible types for this Id, but only in the row where I am, where the select has been changed. How can I do this?

Upvotes: 0

Views: 689

Answers (2)

wayne
wayne

Reputation: 3410

the mighty 'this' in your load(item) function is the scope.

Upvotes: 2

schacki
schacki

Reputation: 9533

I assume that with "ng-repeat scope" are are refering to the parent scope of each individual ng-repeat loop. This can be done easily be referencing the parent scope of the current scope like this:

$scope.$parent

Upvotes: 0

Related Questions