AngularJS angular.forEach不起作用?

时间:2018-07-13 14:37:05

标签: angularjs foreach

我认为这是一个愚蠢的错误,但我仍在努力使之工作,我有一个简单的values变量,其中包含类似以下内容的内容:angular.forEach(scope.values, function(element, key) { console.log(element); console.log(key); });

我想使用angular.forEach这样遍历它:

App\Http\Middleware\EncryptCookies\

scope.values不是空的,看起来像上面给出的示例,但是为什么它不起作用?没有console.log显示任何内容。

1 个答案:

答案 0 :(得分:1)

这里有一个有效的示例:

https://jsfiddle.net/kueduc7s/3/

 var obj = [{
        "item" : "Apples",
        "cost" : 12,
        "actno":12563456,
        "viewer":"asdad",
        "viewer1":"asdad",
        "viewer2":"asdad"

    },
    {
        "item" : "Apples",
        "cost" : 15,
        "actno":44545454,
        "viewer":"asd",
        "viewer1":"sdasd",
        "viewer2":"asdasdadad"
    }];
    angular.module("forEachMod", [])
        .controller("forEachCtrl", function($scope){
            $scope.result = [];

            angular.forEach(obj, function(value, key){
            alert(value.item)
            $scope.result.push({'test1':value.item,'test2':value.cost,'test3':value.viewer,'test4':{"subtest":value.viewer2}})
                console.log($scope.result);
            });
        });