forEach没有达到最后一个指数

时间:2017-10-30 02:31:37

标签: angularjs foreach angularjs-directive promise angularjs-scope

您好我在我的Angular Controller中循环遍历一个数组并将Promise函数传递给每个值,然后为标记为" $ scope [' index_0]的每个值动态创建$ scope模块"例如。

        app.controller('arrCtrl', function($scope, dataService){
            var catList = <%-JSON.stringify(myCats)%>;
            var sequence = Promise.resolve();
            catList.forEach(function(catObj, index){
                sequence = sequence.then(function(){
                    return dataService.getItemsByCategory(catObj._id);
                }).then(function(result){
                    //Create and set scope module to Items array
                    $scope['items_'+index] = result;
                }).catch(function(err){//catch any error
                    console.log(err + ' failed to load!');
                });
            });
            console.log("Controller loaded");
        });

一切顺利,直到我到达最后一个索引。虽然最后一次迭代的$ scope在控制器中被识别,但是当我显示它时它不被识别。

 <p ng-repeat="item in items_2">{{item.Name}}</p>

$ scope [&#39; item_2&#39;]是我的最后一个索引,上面的示例将不会显示。

0 个答案:

没有答案