Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs 6 message success on submit

Tags:

angular6

How can I make a message which can contain "operation successfully" inside my function on submit?

  onSubmit() {
    this.userService.createUser(this.addForm.value)
      .subscribe( data => {
        this.router.navigate(['list-user']);
      });
  }

I'd prefer to append my empty paragraph with my html content.

<div class="col-md-6">
  <form [formGroup]="addForm" (ngSubmit)="onSubmit()">

    <p class="msg_success"></p>

    <button class="btn btn-success">Add user</button>
  </form>
</div>
like image 382
Samir Guiderk Avatar asked Mar 10 '26 16:03

Samir Guiderk


2 Answers

using string interpolation({{...}}) will not give you much control over html you want to display,

You can take advantage of *ngIf if want to display more then just simple string like below:

showMsg: boolean = false;

onSubmit() {
    this.userService.createUser(this.addForm.value)
      .subscribe( data => {
        this.router.navigate(['list-user']);
        this.showMsg= true;
      });
  }

then you can use showMsg boolean to show/hide dive like below

<div class="col-md-6">
  <form [formGroup]="addForm" (ngSubmit)="onSubmit()">

   <div class="row"  *ngIf="showMsg">
        <div class="col-xs-12">
          <p class="alert alert-success">
              <strong>Registration Success!</strong> Please click here to login!.

          </p>
        </div>
      </div>

    <button class="btn btn-success">Add user</button>
  </form>
</div>

That will result in something like below on submit:

enter image description here

note that I am using some bootstrap classes for styling.

like image 69
pk_code Avatar answered Mar 14 '26 00:03

pk_code


You can declare a variable into the component like this:

msg: string = null;

onSubmit() {
    this.userService.createUser(this.addForm.value)
      .subscribe( data => {
        this.router.navigate(['list-user']);
        this.msg = 'success';
      });
  }

Then you can bind msg variable into your template like this:

<div class="col-md-6">
  <form [formGroup]="addForm" (ngSubmit)="onSubmit()">

    <p class="msg_success">{{ msg }}</p>

    <button class="btn btn-success">Add user</button>
  </form>
</div>
like image 31
firegloves Avatar answered Mar 14 '26 01:03

firegloves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!