Just starting out with typescript and angular2 and working through some issues. I have a form that needs to display results from an array of changing items, so I don't know the exact index of each result.
Here is my scenario:
In my form.html file:
<span style="color:red">{{usererror.username}} </span>
...other items here
<span style="color:red">{{usererror.password}} </span>
And in the component:
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
}
}
When calling the above function, the first log returns an array like this:
[{"field":"username","message":"Username cannot be blank."},
{"field":"password","message":"Password cannot be blank."}]
My goal is to assign the username and password variables in usererror based on the returned messages for them to be displayed. However, sometimes the returned results may not include one field or the other. Currently, I am trying to iterate through the message array but facing challenges due to missing fields. Any suggestions would be appreciated as I am still new to angular2 and typescript.