angular - value 属性的属性绑定
问题描述
我是 Angular 5 的新手,我目前正在学习基础知识。我了解了模板引用变量。但我有一个问题。我没有使用模板引用变量,而是尝试使用带有类变量的输入元素的“值”属性进行属性绑定,并在单击按钮时尝试登录类变量,但它不起作用。你能否让我知道我哪里出错了。
这是我的代码
@Component({
selector: 'app-test',
template: `
<input type="text" value="{{greet}}"/>
<button (click)="logMe()">Click </button>
`,
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
public greet="";
constructor() { }
ngOnInit() {
}
logMe()
{
console.log(this.greet);}
}
解决方案
这不会记录任何内容,因为绑定只是单向的。如果要查看greet
变量中的输入值,请ngModel
改用。
对于FormsModule
您的导入app.module.ts
并按以下方式更改代码:
@Component({
selector: 'app-test',
template: `
<input type="text" [(ngModel)]="greet"/>
<button (click)="logMe()">Click </button>
`,
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
public greet="";
constructor() { }
ngOnInit() {
}
logMe()
{
console.log(this.greet);}
}
推荐阅读
- kotlin - 2个相同类型的运算符重载函数
- javascript - 将 gulp@3.9.1 中的 gulp watch 转换为 gulp@4
- python - 从python中的数组中选择3个随机元素
- graphql - Prisma graphql 计算域
- ms-word - 频率是否有 MS Word 通配符?
- javascript - 使用node js替换html文件中的多个字符串
- python - 为什么使用boto3迭代分页器返回的页面如此缓慢?
- javascript - 努力学习乘法
- d3.js - D3 不在 JSON 文件中呈现地图数据
- windows - 是否可以将 roku 设备的操作系统更改为 Windows?