Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载json后添加属性_Json_Angularjs_Angularjs Directive_Angularjs Scope_Http Get - Fatal编程技术网

加载json后添加属性

加载json后添加属性,json,angularjs,angularjs-directive,angularjs-scope,http-get,Json,Angularjs,Angularjs Directive,Angularjs Scope,Http Get,我有一个json: { "info": [ { "id": 999, "products": [ { "id": 1, }, { "id": 2, } ] } ] } Info -- products -----id 我的工厂: AppAngular.factory('model', ['$http', function ($http) { return { load: function (scop

我有一个json:

{
"info": [
{
  "id": 999,
  "products": [
    {
      "id": 1,

    },
    {
      "id": 2,
    }
  ]
}
]
}

Info
-- products
-----id
我的工厂:

AppAngular.factory('model', ['$http', function ($http) {
    return {
        load: function (scope) {
            $http.get('mydomain/api').success(function (data) {
                    var myObject = {};
                    angular.extend(myObject,data);
                    for (var i = 0; i < myObject.info.length; i++) {
                        for (var j = 0; j < myObject.info[i].products.length; j++) {
                        myObject.info[i].products[j].selected = true;
                        myObject.info[i].products[j].quantity = 1;
                        .....
                       }
                    }
                }
            });
        }
    };
} ]);
AppAngular.factory('model',['$http',function($http){
返回{
加载:功能(范围){
$http.get('mydomain/api').success(函数(数据){
var myObject={};
角度扩展(myObject,数据);
对于(变量i=0;i
有一种方法可以在angular.extend之后添加一些属性,而不使用很多for?在获取json后,我们需要向对象添加一些属性或行为。 例如:
Info
--产品
-----身份证
-----精选

-----数量

您可以使用angular.forEach:

AppAngular.factory('model', ['$http', function ($http) {
    return {
        load: function (scope) {
            $http.get('mydomain/api').success(function (data) {
                var myObject = {};
                angular.extend(myObject,data);
                angular.forEach(myObject.info, function (info) {
                    angular.forEach(info.products, function (product) {
                        product.selected = true;
                        product.quantity = 1;
                        .....
                    });
                });
            });
        }
    };
}]);

我正在寻找在我的列表中没有迭代的方法。我不确定那是否可能。。某个地方的某个人需要查看每个产品并添加条目。