简体   繁体   中英

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.

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()">

正如我在评论中所说,您可以尝试将ng-options中的viewables['3d']替换为getViewables(value, '3d')并让控制器返回数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM