ng-repeat以呈现空字符串

时间:2013-07-01 00:25:08

标签: javascript angularjs

我想渲染一组可能的作者作为输入,以便可以编辑分配的作者。

.form-multiple-item(ng-repeat="author in story.authors")
  input(type='text', ng-model="author")

问题是我是否想在现有输入后添加额外的空白输入。我怎么能这样做?

.form-multiple-item(ng-repeat="author in story.authors")
  input(type='text', ng-model="author")
input(type='text', ng-model="author")

例如,这不起作用,因为作者不在正确的范围内。

如果我有story.authors = [""]我想为用户填写一个空白输入。但这不会起作用,因为""只是被ng-repeat忽略而不是渲染空输入。我该如何渲染空输入,或者将另一个ng-model插入到另一个范围内的数组中。

2 个答案:

答案 0 :(得分:1)

我认为Angular的做法是“在模型中加点”。您的authors模型应该是对象而不是字符串:{ name: '' }使用上述对象,您应该能够在ng-repeat中表示空输入。

ng-model中的<input>看起来像这样:

<input type="text" ng-model="author.name" />

答案 1 :(得分:0)

@Harry你问过为什么会让事情复杂化?

原因是任务有效。输入控制器将其值分配给“ng-modal”中的任何内容。如果它是普通变量,则原始列表的链接将丢失。如果它是对象的属性,则保留链接。考虑两种情况:

for author in authors:
   author = "joe"

了解作者如何不被改变?

for author in authors:
   author.name = "joe"

现在保持连接。

这有意义吗?