如何将$ scope对象值添加到另一个$ scope对象? (AngularJS)

时间:2019-01-02 05:14:00

标签: javascript angularjs scope

我要从不同的共享点页面列表中获取数据。我用 Factory 来称呼这些列表。
我的代码正在计算每个列表中有多少个状态为“ 已完成”的项目。
我需要将每个值放入一个数组中,但结果始终是null
这是我的示例:

    <script>
        var myApp = angular.module("myApp", []);
        myApp.factory("myFactory", ["$http", function($http) {
            return {
                siteOne: function() {
                    return $http({
                        method: "GET",
                        url: "siteURL/_api/web/lists/getByTitle('List 1')/items",
                        headers: {"Accept": "application/json; odata=verbose"}
                    });
                },
                siteTwo: function() {
                    return $http({
                        method: "GET",
                        url: "siteURL/_api/web/lists/getByTitle('List 2')/items",
                        headers: {"Accept": "application/json; odata=verbose"}
                    });
                }
            }
        }]);
        myApp.controller("myController", function($scope, $http, myFactory) {
            myFactory.siteOne().success(function(data, status, headers, config) {
                $scope.projects = data.d.results;
                var items = $scope.projects,
                    totalItems = 0;

                for (var i=0;i<items.length;i++) {
                    var currentItem = items[i];
                    if(currentItem.Status!="Completed") {
                        totalItems++;
                    }
                };
                $scope.oneItems = totalItems;
            });

            myFactory.siteTwo().success(function(data, status, headers, config) {
                $scope.projects = data.d.results;
                var items = $scope.projects,
                    totalItems = 0;

                for (var i=0;i<items.length;i++) {
                    var currentItem = items[i];
                    if(currentItem.Status!="Completed") {
                        totalItems++;
                    }
                };  
                $scope.twoItems = totalItems;
            });

            $scope.data = [
                $scope.oneItems, $scope.twoItems
            ];

            console.log(JSON.stringify($scope.oneItems));
            console.log(JSON.stringify($scope.twoItems));
            console.log(JSON.stringify($scope.data));
        });
    </script>

如果我想分别打印每个值,它将显示这些值!但是,如果我尝试将它们放入数组中,它将显示为“ null”值:

3
5
[null, null]

为什么会这样,我该如何解决? ..am我做错什么了吗?

代码更新
这是我的代码已经为那些想要看的人工作了。我使用 $ q 将控制器更改为 Sergey Mell ,也使用了AngularJS v1.7.5(作为 georgeawg 的建议):< / p>

myApp.controller("myController", function($scope, $http, myFactory, $q) {

            $q.all([
                myFactory.siteOne().then(response => {
                    var items = response.data.d.results,
                        totalItems = 0;

                    for (var i=0;i<items.length;i++) {
                        var currentItem = items[i];
                        if(currentItem.Status!="Completed") {
                            totalItems++;
                        }
                    };
                    $scope.oneItems = totalItems;
                }),
                myFactory.siteTwo().then(response => {
                    var items = response.data.d.results,
                        totalItems = 0;

                    for (var i=0;i<items.length;i++) {
                        var currentItem = items[i];
                        if(currentItem.Status!="Completed") {
                            totalItems++;
                        }
                    };  
                    $scope.twoItems = totalItems;
                })
            ]).then(function() {
                $scope.data = [
                    $scope.oneItems, $scope.twoItems
                ];

                console.log(JSON.stringify($scope.data));
            });
        });

3 个答案:

答案 0 :(得分:1)

您要在请求完成之前放入数据。您必须等到两个请求结束。我建议使用Promise.all$q.all,这对AngularJS来说更自然,即:

myApp.controller("myController", function($scope, $http, myFactory, $q) {
        $q.all([
           myFactory.siteOne().then(/* Your stuff here */), 
           myFactory.siteTwo().then(/* Your stuff here */), 
        ]).then(function() {
           $scope.data = [
             $scope.oneItems, $scope.twoItems
           ];
        })

答案 1 :(得分:0)

还为键分配值,例如:

$scope.data = [
dataItemOne: $scope.oneItems, 
dataItemTwo: $scope.twoItems ];

答案 2 :(得分:-1)

您最好选择,因为下面给出的@Sergey Mell使用$ q服务。
如果您不想使用$ q。 然后
声明$ scope.data = [];在顶部并使用
$ scope.data.push($ scope.oneItems)
但是请确保在顶部将[$]声明为$ scope.data,否则将引发错误。
您的控制器代码将类似于

  myApp.controller("myController", function($scope, $http, myFactory) {
        $scope.data = [];
        myFactory.siteOne().success(function(data, status, headers, config) {
            $scope.projects = data.d.results;
            var items = $scope.projects,
                totalItems = 0;

            for (var i=0;i<items.length;i++) {
                var currentItem = items[i];
                if(currentItem.Status!="Completed") {
                    totalItems++;
                }
            };
            $scope.oneItems = totalItems;
           $scope.data.push($scope.oneItems);
        });

        myFactory.siteTwo().success(function(data, status, headers, config) {
            $scope.projects = data.d.results;
            var items = $scope.projects,
                totalItems = 0;

            for (var i=0;i<items.length;i++) {
                var currentItem = items[i];
                if(currentItem.Status!="Completed") {
                    totalItems++;
                }
            };  
            $scope.twoItems = totalItems;
             $scope.data.push($scope.twoItems );
        });

      /*  $scope.data = [
            $scope.oneItems, $scope.twoItems
        ];*/

        console.log(JSON.stringify($scope.oneItems));
        console.log(JSON.stringify($scope.twoItems));
        console.log(JSON.stringify($scope.data));
    });