简体   繁体   English

如何使有角度的子范围刷新?

[英]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 . 并且,当使用新数据集更新控制器中的数据(在顶部ng-repeat使用的data属性)时,它不会自动刷新ng-options的子作用域。 which are derived from the data set. 从数据集中得出的。 Does anyone know how to refresh child scopes ? 有谁知道如何刷新子scopes I have tried calling $apply and $digest but with no success. 我尝试调用$apply$digest但是没有成功。

If you look at the docs for ngInit , you'll see they basically say "never use this except in nested ngRepeats ". 如果您查看ngInit文档 ,您会发现他们基本上说“除了嵌套的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. 一种执行所需操作的方法是为每个重复项创建一个控制器,并在更改的数据(在这种情况下为表达式)上创建一个$watch ,并设置viewables相等的viewables

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')并让控制器返回数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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