Angularjs推送具有不同值的项目

时间:2015-04-30 08:14:46

标签: javascript html angularjs

我有一个json,其中我有一些值使用ng-repeat显示在列表中。当我单击列表中的项目时,我将项目添加到一个空数组中,我循环使用push方法创建表单的一些文件。用户可以创建他想要从第一个数组中单击的所有字段。问题是,如果我在表单的输入文本中按下两个或多个相同的项目,它会在所有相同的输入中重复。我需要的是所有领域都是独立的,即使是相同的。我在这里创建了一个jsfiddle:https://jsfiddle.net/k6s357L8/8/感谢您的帮助



var myApp = angular.module('myApp',[]);

myApp.controller("mycontroller", ["$scope", "$http",
    function($scope, $http){
        $scope.getItems = {
    "data": [
        {
            "label": "first",
            "objects": [
                {
                    "name": "firstObj",
                    "attributes": [
                        {
                            "attrname": "asd1",
                            "attrValue": "",
                            "attrType":"text"
                        },
                        {
                            "attrname": "asd2",
                            "attrValue": "",
                            "attrType":"text"
                        }
                    ]
                }
            ],
            "key": "bolla"
        },
        {
            "label": "second",
            "objects": [
                {
                    "name": "secondObj",
                    "attributes": [
                        {
                            "attrname": "asd",
                            "attrValue": "",
                            "attrType":"text"
                        },
                        {
                            "attrname": "asd3",
                            "attrValue": "",
                            "attrType":"text"
                        }
                    ]
                }
            ],
            "key": "2"
        }
    ]

};
    $scope.filterSelected = $scope.getItems.data[0].objects;
        
        $scope.myNewArray = {
            newArray: [
            
            ]
        }
        $scope.pushItems = function pushItems(items) {
            $scope.myNewArray.newArray.push(items);
            console.log($scope.myNewArray);
        }
        
}]);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp' ng-controller='mycontroller'>

    <div data-ng-repeat="item in myNewArray.newArray track by $index">
        <div ng-if="item.attrType == 'text'">
            <input id="form-f{{$index}}" type="text" placeholder="{{item.attrname}}" data-ng-model="item.attrValue"/>
        </div>
    </div>
    <div data-ng-repeat="object in getItems.data">
        <div data-ng-repeat="att in object.objects">
            <ul ng-repeat="data in att.attributes">
                <li>
                    <a ng-click="pushItems(data)">{{data.attrname}}</a>
                </li>
            </ul>
        </div>
        
    </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

您可以使用angular.copy来复制对象:

$scope.pushItems = function pushItems(items) {
    $scope.myNewArray.newArray.push(angular.copy(items));
    console.log($scope.myNewArray);
}

请参阅此fiddle