简体   繁体   English

如何将对象添加到Angular中另一个动态创建的对象中的键中?

[英]How to add objects to a key within another dynamically created object in Angular?

I'm using Angular to create a list app. 我正在使用Angular创建列表应用。 For testing purposes I have two pre-created lists each with pre-created names and items. 出于测试目的,我有两个预先创建的列表,每个列表都包含预先创建的名称和项目。 Eventually the user will initially be presented with an empty list to which they can add items. 最终,最初将向用户显示一个空白列表,用户可以在其中添加项目。 Then they can add a new list for new items. 然后他们可以为新项目添加新列表。 So far I have the ability to add and remove items to and from the existing list, and I can add a new list. 到目前为止,我可以在现有列表中添加和删除项目,也可以添加新列表。 But I can't add new items to a newly created list. 但是我无法将新项目添加到新创建的列表中。 I get a "Cannot read property 'push' of undefined" error for the line list.items.push(this.item); 对于行list.items.push(this.item);出现“无法读取未定义的属性'push'”错误list.items.push(this.item); . My guess is that when I create a new list, there is no items key added to it and that's why it's undefined. 我的猜测是,当我创建一个新列表时,没有添加items ,这就是为什么它是未定义的。 I tried creating an items key when I create a new list, but I couldn't get it to work. 创建新列表时,我尝试创建items键,但无法正常工作。 That might not be the problem anyway. 无论如何这可能不是问题。

https://codepen.io/anon/pen/oWRQvQ https://codepen.io/anon/pen/oWRQvQ

Here's the markup: 这是标记:

<div ng-app="notepadApp">
    <div ng-controller="notepadController as notepadCtrl">
        <header ng-repeat="list in notepadCtrl.lists">
            <div>Delete list</div>
            <h1>{{list.name}}</h1>
        </header>
        <div ng-repeat="list in notepadCtrl.lists" class="shoppingList" ng-controller="ItemController as itemCtrl">
            <ul>
                <li ng-repeat="item in list.items">

                    <label>
                        <input type="checkbox" ng-model="item.checked">
                        {{item.name}}
                    </label>
                    <form name="itemForm" ng-submit="itemCtrl.removeItem(list, $index)">
                        <input type="submit" value="remove">
                    </form>

                </li>
            </ul>
            <form name="itemForm" ng-submit="itemCtrl.addItem(list)">
                <input type="text" ng-model="itemCtrl.item.name">
                <input type="submit" value="Add item">
            </form>
        </div>

        <form name="addListForm" ng-submit="notepadCtrl.addList(lists)">
            <input type="text" ng-model="notepadCtrl.list.name">
            <input type="submit" value="Add list">
        </form>
    </div>
</div>

This script is: 该脚本是:

(function(){

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

var shoppingLists = [
    {
        name: 'groceries',
        items: [
            {
                name: 'milk',
                checked: false
            },
            {
                name: 'eggs',
                checked: false
            }
        ]
    },
    {
        name: 'cvs',
        items: [
            {
                name: 'pills',
                checked: false
            },
            {
                name: 'cotton balls',
                checked: false
            },
            {
                name: 'razors',
                checked: false
            }
        ]
    }
];


app.controller('notepadController', function(){
    this.lists = shoppingLists;
    this.list = {};

    this.addList = function() {
        this.lists.push(this.list);
        this.list = {};
    };

});

app.controller('ItemController', function(){
    this.item = {};

    this.addItem = function(list){
        list.items.push(this.item);
        this.item = {};
    };

    this.removeItem = function(list, index) {
        list.items.splice(index, 1);
    };
});
})();

You create a new list in two places; 您在两个地方创建一个新列表; once on controller initialization, and again in the addList function. 一次在控制器初始化时,再次在addList函数中。

In both places, you need an items array to match the structure being used by the other list. 在这两个地方,都需要一个items数组来匹配另一个列表使用的结构。 In both places where you create a new list, change your initialization code to this.list={items:[]}; 在创建新列表的两个地方,都将初始化代码更改为this.list={items:[]}; in order to ensure that there is an items [] to push into. 为了确保有要推送的items []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM