0

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

.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 插入另一个范围内的数组中。

4

2 回答 2

1

我认为 Angular 这样做是为了“在你的模型中添加一个点”。您的authors模型应该是一个对象而不是字符串:{ name: '' }使用上述对象,您应该能够在ng-repeat.

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

<input type="text" ng-model="author.name" />
于 2013-07-01T01:00:59.360 回答
0

@Harry You asked why complicate things?

The reason is so that assignments work. The input controller will assign its value to whatever's in "ng-modal". If it's a normal variable, then the link to the original list will be lost. If it's an attribute of an object, then the link is preserved. Consider the two scenarios:

for author in authors:
   author = "joe"

See how authors doesn't get changed?

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

Now the connection is preserved.

Does that make sense?

于 2013-07-28T01:29:44.317 回答