admin管理员组文章数量:1415645
I have an object called user
:
let user = {};
How can I bind an input to a undefined property? E.g.
<input [(ngModel)]="user.name" placeholder="Enter your name"></input>
And whenever the user enters his name, I want the ngModel
to create the undefined property of that object and result in this:
user = {name: "John"};
Is this also possible to be done at deepter levels? E.g.
<input [(ngModel)]="user.homeaddress.postcode" placeholder="Enter your post code"></input>
to result in:
user = {name: "John", homeaddress: {postcode: "E20 1QS"}};
I have an object called user
:
let user = {};
How can I bind an input to a undefined property? E.g.
<input [(ngModel)]="user.name" placeholder="Enter your name"></input>
And whenever the user enters his name, I want the ngModel
to create the undefined property of that object and result in this:
user = {name: "John"};
Is this also possible to be done at deepter levels? E.g.
<input [(ngModel)]="user.homeaddress.postcode" placeholder="Enter your post code"></input>
to result in:
user = {name: "John", homeaddress: {postcode: "E20 1QS"}};
Share
Improve this question
edited Aug 28, 2018 at 23:52
EDJ
asked Aug 28, 2018 at 23:46
EDJEDJ
1,0334 gold badges20 silver badges43 bronze badges
2 Answers
Reset to default 3Two way data binding supports any nested objects, you just need to use it as,
<input [(ngModel)]="user.homeaddress.postcode" placeholder="Enter your post code"></input>
since you have initialized data it wont throw any error.
Is this also possible to be done at deepter levels?
Yes. "user.homeaddress.postcode"
will work.
本文标签: javascriptTypescriptBind ngModel to property of objectStack Overflow
版权声明:本文标题:javascript - Typescript - Bind ngModel to property of object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745242580a2649410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论