动态将项目添加到angularjs ng-repeat [英] dynamically add item to angularjs ng-repeat

查看:64
本文介绍了动态将项目添加到angularjs ng-repeat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条指令,该指令生成具有添加和删除功能的拖放可重新排序列表.如果单击该容器,则会动态添加输入,然后在其中输入内容,并在输入逗号时将输入的值推入与ng-repeat一起使用的列表中以构建列表.(应该对此站点的用户有点熟悉:))

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 :) )

当支持它的初始对象不为null时,此方法很棒.但是,当对象开始为null并尝试添加第一项时(通过检测null和scope.$应用初始化),不会生成标记.

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.

这很容易说明我的意思. http://plnkr.co/edit/Momlgpfy82kHRPwXGR8V?p=preview

Here is a plunk to show what I mean. 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)?

推荐答案

将列表设置为空数组...当将新项推入数组时,角度侦听器将更新指令

Set list to empty array...whenevr a new item is pushed into array angular listeners will update directive

http://plnkr.co/edit/h3GOpTX6Chh1wjcM9QrV?p=preview

这篇关于动态将项目添加到angularjs ng-repeat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆