第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 ngModel 和 Angular 無法在字符串上創(chuàng)建屬性“selected”

使用 ngModel 和 Angular 無法在字符串上創(chuàng)建屬性“selected”

蕪湖不蕪 2024-01-22 17:08:53
所以我嘗試[(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>Myaccounts已創(chuàng)建并填充組件:accounts: Account[] = [];我的Account對(duì)象:export class Account {name: string;surname: string;}我越來越ERROR TypeError: Cannot create property 'selected' on string 'John'我看到了這個(gè)帖子:Cannot create property 'selected' on string 'Information Technology' angularjs但不太明白如何將提到的修復(fù)應(yīng)用于我的案例,也許是因?yàn)槲沂褂玫氖?Angular 而不是 AngularJS。如果有人能幫助理解這里的問題,我會(huì)很高興嗎?
查看完整描述

1 回答

?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊

看來您需要selected在數(shù)組中擁有屬性。因此,為了避免污染Account類,您可以使用map方法:


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>

更新:


您可以使用方法filter來獲取所有選定的值:


let onlySelected = this.withSelectedProperty.filter(f => f.selected);


查看完整回答
反對(duì) 回復(fù) 2024-01-22
  • 1 回答
  • 0 關(guān)注
  • 126 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)