gpt4 book ai didi

javascript - Angular 7 : How to keep the variable value updated in html after changes in ts file

转载 作者:行者123 更新时间:2023-11-30 14:05:27 28 4
gpt4 key购买 nike

我有一个在页面加载时传递给 html 的值,但是这个值在我使用其他函数时发生变化,然后该变量不再在 html 中更新。

我尝试使用 ngModel,但它没有用,因为我有一个对象数组和一个对象。

html

<div *ngFor="let item of itens">            
<textarea class="textarea" id="item-textarea" [value]="item.text">
</textarea>
</div>

public itens:any[] = [];

constructor() {

}

ngOnInit() {
this.initItens();
this.changeItens();
}

initItens() {
let item = new Item();
item.text = "Hello world";
return itens.push(item);
}

changeItens() {
let item = new Item();
item.text = "Bye bye World";
return itens.push(item);
}

我想知道如何在 .ts 文件中更新变量 itens 和 item (item.text) 时始终在 html 中更新

谢谢。

最佳答案

为什么你的函数返回数组?应该是这样的

initItens() {
let item = new Item();
item.text = "Hello world";
this.itens.push(item);
}

changeItens() {
let item = new Item();
item.text = "Bye bye World";
this.itens.push(item);
}

关于javascript - Angular 7 : How to keep the variable value updated in html after changes in ts file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55497761/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com