Daniel Robinson
Daniel Robinson

Reputation: 14848

How to make angular child scopes refresh?

I have a template:

<div class="oss-object cp-def cp-row" ng-repeat="(key, value) in data" ng-init="viewables = getViewables(value.versions[0].objectInfo);">
        <div class="cp-def">
            {{value.info.name}} OssStatus: {{value.versions[0].objectInfo.status}}
            , 3D-Viewables:
                <select ng-change="viewableSelected(value.versions[0].urn, viewables, '3d')" ng-model="viewables.selected3d" ng-options="viewable as viewable.name for viewable in viewables['3d']">
                    <option value="">---Please select---</option>
                </select>
            , 2D-Viewables:
                <select ng-change="viewableSelected(value.versions[0].urn, viewables, '2d')" ng-model="viewables.selected2d" ng-options="viewable as viewable.name for viewable in viewables['2d']">
                    <option value="">---Please select---</option>
                </select>
        </div>
    </div>

And when data gets updated (the data property used in the top ng-repeat) in my controller with a new data set, it doesn't automatically refresh the child scopes in the ng-options. which are derived from the data set. Does anyone know how to refresh child scopes? I have tried calling $apply and $digest but with no success.

Upvotes: 0

Views: 175

Answers (2)

Omri Aharon
Omri Aharon

Reputation: 17064

As I said in the comments, you could try to replace viewables['3d'] in the ng-options with getViewables(value, '3d') and have the controller return the array.

Upvotes: 1

Dylan Watt
Dylan Watt

Reputation: 3387

If you look at the docs for ngInit, you'll see they basically say "never use this except in nested ngRepeats". One way to do what you want is to create a controller for each repeated item, and create a $watch on the data that changes (or expression in this case), and set viewables equal to that.

eg

function ViewablesCtrl($scope){
   $scope.$watch($scope.getViewables, function(){$scope.viewables = $scope.getViewables($scope.value.versions[0].objectInfo);}
})

--

<div class="oss-object cp-def cp-row" ng-repeat="(key, value) in data" ng-controller="ViewablesCtrl()">

Upvotes: 1

Related Questions