javascript 将项目动态添加到 angularjs ng-repeat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15891052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dynamically add item to angularjs ng-repeat
提问by μBio
I have a directive that generates a drag/drop reorderable list with add and remove functionality. If you click in the container, an input is added dynamically, you type in it and when you type a comma, the value you typed is pushed into the list used with ng-repeat to build the list. (Should be kinda familiar to users of this site :) )
我有一个指令,它生成一个带有添加和删除功能的拖放可重新排序列表。如果您在容器中单击,则会动态添加一个输入,您输入它,当您输入逗号时,您输入的值将被推送到与 ng-repeat 一起使用以构建列表的列表中。(这个网站的用户应该有点熟悉:))
This works awesome when the initial object backing it is not null. But when the object begins null and you try to add the first item (by detecting the null and scope.$apply the initialization)the markup is not generated.
当支持它的初始对象不为空时,这非常有效。但是当对象开始为空并且您尝试添加第一项时(通过检测空值和范围。$应用初始化)不会生成标记。
Here is a plunk to show what I mean. http://plnkr.co/edit/Momlgpfy82kHRPwXGR8V?p=preview
这里有一个 plunk 来说明我的意思。 http://plnkr.co/edit/Momlgpfy82kHRPwXGR8V?p=preview
In my app, the data is coming from an external source, so I can't ensure non-null lists. How can I get angular to properly respond to the array initialization (and later push)?
在我的应用程序中,数据来自外部源,因此我无法确保非空列表。如何获得 Angular 以正确响应数组初始化(以及稍后推送)?
回答by charlietfl
Set list to empty array...whenevr a new item is pushed into array angular listeners will update directive
将列表设置为空数组...当新项目被推入数组时,角度侦听器将更新指令