javascript - 第一次提交按钮单击后,我看不到双向绑定值
问题描述
我正在尝试将输入中的项目添加到 div 中id = items
。
它似乎在第一次提交点击时有效,但之后失败。
我观察到在第一次提交点击后,双向数据绑定似乎消失了。
HTML 代码:
<form>
<input type="text"name="item"
[(ngModel)]="test.text">
<br>
<p>{{test.text}}</p> <!--disappears after the first
button click-->
<input type="submit" value="Add"
(click)="addItem()">
</form>
<div id="items" *ngFor="let goal of goals">
<p>
{{ goal.text }}
</p>
<p>
{{ goal.num }}
</p>
</div>
在我的组件文件中,我试图将用户输入推送到一个数组中并使用ngFor
上面的语法循环它,但它在第一个结果后返回空。
goals = [];
test: any = {text: "ok", num: "789"};
addItem() {
this.goals.push(this.test);
this.test = '';
this.itemCount = this.goals.length;
}
我哪里会出错?提前致谢。
解决方案
在您的 addItem 函数中只需更改此
addItem() {
this.goals.push(this.test);
this.test = '';
this.itemCount = this.goals.length;
}
至
addItem() {
this.goals.push({...this.test});
this.test.text = '';
this.itemCount = this.goals.length;
}
推荐阅读
- android - React-native Android 发布版本损坏(UI 导航中断没有错误)- 在调试版本中正常工作
- r - 访问目录中的所有文件并使用 r studio 执行 rscript
- node.js - Node JS 的问题和承诺在完成前返回
- pandoc - Pandoc:每个部分的单独目录
- android - 如何以编程方式在我的应用程序中创建 APK?
- c# - Check in Azure Functions if client is still connected to SignalR Service
- react-spring - react-spring@next - 一个带有异步功能的弹簧作为“to”动画每个渲染
- sparql - Apache Jena 在使用 VALUES 块执行 SPARQL 查询时引发异常
- c - 在C中的char []之前和之后附加'
- python - 查找和替换熊猫数据框中的坏字符