Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 无法创建属性';选定的';在字符串上,使用带角度的ngModel_Html_Angular_Angular Ngmodel - Fatal编程技术网

Html 无法创建属性';选定的';在字符串上,使用带角度的ngModel

Html 无法创建属性';选定的';在字符串上,使用带角度的ngModel,html,angular,angular-ngmodel,Html,Angular,Angular Ngmodel,所以我试着这样使用[(ngModel)]: <div class="items"> <tbody> <tr *ngFor="let account of this.accounts"> <td> <input type="checkbox" [(ngModel)]="this.account.name.selected" name="account"> {{

所以我试着这样使用
[(ngModel)]

  <div class="items">
    <tbody>
    <tr *ngFor="let account of this.accounts">
      <td>
          <input type="checkbox" [(ngModel)]="this.account.name.selected" name="account">
              {{account.name}}
      </td>
    </tr>
    </tbody>
  </div>
我收到
错误类型错误:无法在字符串“John”上创建属性“selected”


我看到了这个帖子:但我不太明白如何将上面提到的修复应用到我的案例中,可能是因为我使用的是AngularJS,而不是AngularJS。如果有人能帮助您理解这里的问题,我们将非常高兴。

看起来您需要在数组中选择
属性。因此,为了避免污染
账户
类,您可以使用
映射
方法:

let withSelectedProperty = this.accounts.map(s=> ({...s, selected: false}));
和HTML:

<tr *ngFor="let account of withSelectedProperty">
  <td>
      <input type="checkbox" [(ngModel)]="account.selected" name="account">
          {{account.name}}
  </td>
</tr>

这里的名称是
string
。它不包含所选的属性
。如果它是一个对象,那么你可以定义一个属性
选中的
。是的,我从另一个线程中理解了这一点,但是我如何使它工作呢?哦,我刚刚从
ngModel
中删除了
。名称
,现在它是对象,得到了,非常感谢!该复选框将为其绑定到的任何对象指定一个布尔值。因此,现在您将在
帐户
实例上拥有一个布尔属性。复选框最初将被取消选中,因为
selected
最初是未定义的。您需要将类型从字符串更改为对象,并为此声明所需的属性。如果我只是将其用作对象,如
[(ngModel)]=“this.account.selected”
?它似乎起作用了。但如何访问选定的值?例如,如果我想
console.log()
组件中选定的元素?好的,得到了,但不应该有
[(ngModel)]=“this.account.selected”
名称
字段已过时,否?@developer1哦,我很忙,请查看我的更新答案OK,现在为什么要删除
?@developer1,因为
<tr *ngFor="let account of withSelectedProperty">
  <td>
      <input type="checkbox" [(ngModel)]="account.selected" name="account">
          {{account.name}}
  </td>
</tr>
let onlySelected = this.withSelectedProperty.filter(f => f.selected);