简体   繁体   English

AngularJS使用ng-repeat缺少ng-init的作用域值

[英]Angularjs missing scope values for ng-init using ng-repeat

Hi I have following html in my page 嗨,我的页面中有以下HTML

<div ng-repeat="a in loc" ng-init="getValues(a)">
          <div>
           <select  ng-options="c.id as c.site for c in names" ng-model=a.id></select>
         </div>
           <div>
           <select  ng-options="a.id as a.name for a in val"></select>
         </div>
   </div>

In my controller I have 在我的控制器中

   $scope.getValues = function(a) {
        $scope.val = pRepository.getVals.query({ id: a.ie }, function (data) {
            $scope.val = data;
       });

Problem I have is when "loc" list in ng-repeat is small with one or two records it works fine. 我遇到的问题是,当ng-repeat中的“ loc”列表很小,只有一两个记录时,它工作正常。 When it is long with 20 records or so it misses bunch of $scope.val values and lists some of them. 当它的长度超过20条记录时,它将丢失$ scope.val值束并列出其中的一些值。 I tried using $promise but no luck. 我尝试使用$ promise,但是没有运气。 Please let me know how can i slow down ng-repeat or something so it loads all the vals. 请让我知道如何减慢ng-repeat或其他速度,以便它加载所有val。 Thanks 谢谢

Every time getValues(...) is called the same $scope.val property is updated (it is the val property of the parent of the scope created by ngRepeat . Since you need to have a "local" val property, you should modify the code accordingly: 每一次getValues(...)被称为相同 $scope.val属性更新(这是val通过创建范围的父母财产ngRepeat 。既然你需要有一个“本地” val属性,则应修改相应的代码:

<div ng-repeat="a in loc" ng-init="values = getValues(a)">
    ...
    <div>
        <select ng-model="someModel" ng-options="a.id as a.name for a in values">
        </select>
    </div>
</div>

$scope.getValues = function(a) {
    var data = [];
    pRepository.getVals.query({id: a.ie}, function (data) {
        data = data;
    });
    return data;
}

See, also, this short demo . 另请参见此简短演示

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

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