JMon
JMon

Reputation: 3447

display error message on form if the message is not empty angular 2

I have a register form and I wish to display an error message on the form, in case something went wrong.

In my .ts file, I have the following code :-

onSubmit(email, password, name, surname, username, homephonenumber, mobilenumber){
this._userService.register(email, password, name, surname, username, homephonenumber, mobilenumber)
  .subscribe((result) => {
    if (result.success == "True") {
      //this._userService.login(email, password);
    }
    else
    {
      this.errorMessage = result.message;
    }
});

}

And in my form I have the following:-

<div class="alert alert-danger" ng-if="{{errorMessage}}"></div>

This is throwing an error in the console:-

Can't bind to 'ng-if' since it isn't a known property of 'div'

How can I get the error message to display?

Thanks for your help and time.

Upvotes: 1

Views: 3797

Answers (1)

Suraj Rao
Suraj Rao

Reputation: 29614

ng-if is angularjs syntax. For angular 2 change it to :

<div class="alert alert-danger" *ngIf="errorMessage"></div>

Angular 2 template docs : link

Upvotes: 1

Related Questions