Angular-具有多重绑定的TextArea



我的屏幕上有两个输入字段("名称"one_answers"城市"(和一个文本区域。文本区域被一些JSON数据填充,并且数据包含一些细节;name"城市"地址"pin码";等

我如何只更新";name";或";城市;当用户在"上键入内容时,在文本区域内;name";或";城市;输入字段。

有什么东西可以对textarea进行多重绑定吗?

如有任何帮助,我们将不胜感激。

这种场景没有多个绑定可用,但是,您可以在每次更改事件时解析json,并更新相关字段:

参见演示

@Component({
selector: "hello",
template: `
<textarea (change)="updateObj()" [(ngModel)]="objJson"></textarea>
{{ obj | json }}
`,
styles: [
`
h1 {
font-family: Lato;
}
`
]
})
export class HelloComponent {
obj = {
name: "John",
city: "Chicago"
};
objJson: string;
constructor() {
this.objJson = JSON.stringify(this.obj);
}
updateObj() {
const tempObj = JSON.parse(this.objJson);
this.obj.name = tempObj.name;
this.obj.city = tempObj.city;
// also possible for a generic update:
// Object.keys(this.obj).forEach(k => { this.obj[k] = tempObj[k]; });
}
}

最新更新