【发布时间】:2017-01-06 10:09:49
【问题描述】:
我是 typescript 和 angular2 的新手,正在学习这个,我遇到了这个问题。我有一个表单,我想显示一组项目的结果,但这些项目每次都在变化,所以我现在没有返回结果的实际索引。
是这样的
我有这个form.html
<span style="color:red">{{usererror.username}} </span>
...other items here
<span style="color:red">{{usererror.password}} </span>
现在在组件中
export class LoginComponent implements OnInit{
public usererror={
username:"",
password:""
}
//this function is called after a post request
onLoginError(error:any){
let message= JSON.parse( error._body);
console.log(error._body); //first log
console.log(message) //second log
}
}
与上述函数一样,第一个日志返回表单的数组
[{"field":"username","message":"Username cannot be blank."},
{"field":"password","message":"Password cannot be blank."}]
现在我想在上面的 usererror 中分配用户名和密码变量,以便它们可以显示
像这样的
onLoginError(error:any){
let message= JSON.parse( error._body);
let message= JSON.parse( error._body);
for (var i=0; i<message.length; i++){
this.usererror.message[i].field = message[i].message; //this fails since the
//usererror doesnt have (message[i].field) property
}
}
有时返回的结果没有用户名字段,有时没有密码字段。我该怎么做,我还是 angular2 和 typescript 的新手
【问题讨论】:
标签: javascript arrays angular typescript