ng重复以呈现空字符串

ng-repeat to render empty string

本文关键字:字符串 ng      更新时间:2023-09-26

我想呈现一个可能的作者数组作为输入,以便可以编辑指定的作者。

.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模型。

我认为Angular的方法是"在模型中放一个点"。您的authors模型应该是一个对象而不是字符串:{ name: '' }有了上述对象,您应该能够在ng-repeat中表示一个空输入。

<input>中的ng-model如下所示:

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

@Harry你问为什么要把事情复杂化?

原因是作业能起作用。输入控制器将其值分配给"ng模态"中的任何值。如果它是一个普通变量,那么到原始列表的链接将丢失。如果它是一个对象的属性,那么链接将被保留。考虑两种情况:

for author in authors:
   author = "joe"

看看作者是如何不被更改的?

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

现在,连接被保留。

这有道理吗?