Reference new scope created by adding an object to ng-repeat

103 views Asked by At

I have a set of items in a model tree, which are shown via ng-repeat. Each item in ng-repeat has its own controller (which lets each item have its own properties). Each item has a selected property that is only important per-session, so I'm not saving it as an attribute on the model tree, which is synced to a server.

function ItemCtrl($scope) {

    $scope.selected=false;

    $scope.Select = function () {
         $scope.selected = true;
    };

};

Now, when I create a new item by adding it to the model tree, I want to access its scope in the entry created automatically by ng-repeat in order to flip its "selected" variable to true, but I don't know how to access that. I've made a quick fiddle to illustrate my problem. Thoughts?

1

There are 1 answers

1
user4321 On

You can do it with the $rootScope. Have updated the fiddle link.

Add $rootScope to the ItemCtrl and the Select function.

function ItemCtrl($scope,$rootScope) {

    $scope.selected=false;

    $rootScope.Select = function () {
         $scope.selected = true; 
         alert("selected is set to true");
    };

};

updated link to the fiddle