[英]Using ngModel to bind dynamically generated inputs
I'm fairly new to Angular so bear with me if I have some rookie mistakes here :) I am dynamically adding inputs based on some properties that need to be filled out (this.properties) and I want to bind each of those dynamic inputs to some object (this.selectedProperties = {}) so that I end up with something like this. 我是Angular的新手,所以如果我在这里遇到一些菜鸟错误,请多多包涵:)我正在根据需要填写的某些属性(this.properties)动态添加输入,并且我想绑定每个动态输入到某个对象(this.selectedProperties = {}),这样我就得到了类似的结果。
this.properties = [new MyData { name = "theName", examples = "theExamples" },
new MyData { name = "anotherName", examples = "moreExamples" }];
this.selectedProperties = { "theName": "valueFromBinding", "anotherName": "anotherValueFromBinding" }
Here's a slimmed down version of what I have so far. 这是我到目前为止的精简版本。 Everything is working other than the ngModel to set the selected values. 除了ngModel以外,其他所有功能都无法设置所选值。
<div *ngFor="let property of this.properties">
<label>{{property.name}}</label>
<input type="text" placeholder="{{property.examples}}"
[ngModel]="this.selectedProperties[property.name]"
name="{{property.name}}Input">
</div>
Thanks! 谢谢!
Try like this : 尝试这样:
component.html component.html
<div *ngFor="let property of properties">
<label>{{property.name}}</label>
<input type="text" placeholder="{{property.examples}}" [ngModel]="selectedProperties[property.name]" name="{{property.name}}Input">
</div>
component.ts component.ts
export class AppComponent {
properties = [
new MyData { name: "theName", examples: "theExamples" },
new MyData { name: "anotherName", examples: "moreExamples" }
];
selectedProperties = {
"theName": "valueFromBinding",
"anotherName": "anotherValueFromBinding"
};
}
事实证明,我需要两种方式的绑定,以便当用户在用户界面中对其进行更改时,selectedProperties [property.name]会更新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.