[英]How to bind an array to ngModel in angular and detect the changes
我有一個字符串數組
array = ["one","two","three","four"]
,我想將此值綁定到這樣的輸入:
<input class="form-control"
type="text"
[(ngModel)]="array">
然后當用戶更改輸入中的某些內容時,例如添加“五個”,我想將其添加到數組中,與刪除相同。 我已經像我之前展示的那樣做了,起初輸入有數組 [4],但是當我開始在輸入中輸入一些東西時,Angular 將其更改為字符串一、二、三、四、五 我怎樣才能綁定到只更新原始數組而不將其轉換為字符串?
1) 不能用文本輸入字段綁定數組
2) 當文本字段中的值發生變化時觸發事件
<input class="form-control" type="text" (change)="add($event.target.value)">
3)在函數上只需將該值添加到數組中
add(value){
this.array.push(value)
console.log(this.array)
}
.ts 文件
export class AppComponent {
array = ["one","two","three","four"]
add(value){
this.array.push(value)
console.log(this.array)
}
}
.html
<input class="form-control" type="text" (change)="add($event.target.value)">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.