我正在为一个项目学习 angular-2,我不知道这个错误。 这是我的 html 文件。我想从这些字段中获取值。任何想法
<form> <label> Name: </label><br /> <input type="text" name="name" [(ngModel)]= "user.name" /> <br/> <label> Id: </label><br/> <input type="number" name="Id" [(ngModel)]= "user.Id" /><br/> <label> Email: </label><br /> <input type="text" name="email" [(ngModel)]= "user.email" /><br/> </form>
这是在我的 .ts 文件中
export class UserComponent{
user:User; constructor(){ } }
}
// interface
interface User{
name:string;
Id:number;
email:string; }
我无法在我的 Html 文件上执行类似 {{user.name}} 的操作。为什么 ?我做错了吗?
最佳答案
可能是因为您没有将user
变量初始化为一个对象?试试这个:
export class UserComponent {
user: User = {
name: "",
Id: null,
email: ""
};
constructor() { }
}
关于javascript - Angular 2 中的绑定(bind)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47125157/