0

I'm new to Angular. So I may not be able to frame the question correctly. I apologize in advance. I have also tried this question. I have created a login form for my MEAN stack application. Its a simple login with email and password field. My typescript file decides whether the login was successful or not. Here is my code.

login.component.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth.service';
...

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  flag= {
    valid: true
  }

  ...

  constructor( private _auth: AuthService, private _router: Router) {
  }

  ngOnInit() {
  }


  onSubmit() {
    this._auth.loginUser(this.loginUserData)
    .subscribe(
      res => {
        ...
      },
      err => {
        this.flag.valid=false,
        document.querySelector('#login-denied').innerHTML="hello";
      }
    )
  }
}

In the above file, flag is a boolean variable. True means values are OK and False otherwise.

Here is my html code login.component.html

<form>
  <div class="form-group">
      USERNAME FIELD
  </div>

  <div class="form-group">
      PASSWORD FIELD
  </div>
  <button type="submit" (click)="onSubmit()">Submit</button>

  <span *ngIf="flag.valid===false" id="login-denied">
      <i class="fa fa-times-circle" style="color:firebrick;"></i>
  </span>
</form>

Please pay more attention to my span tag. Please correct me. I am getting

"document.querySelector(...) is null"

And here is a screenshot also.

As you can see. The fa icon is there. But no message.

2 Answers 2

2

In your login component add a string property "message"

export class LoginComponent implements OnInit {
  message: string = '';
  …
}

In your submit method

onSubmit() {
  this._auth.loginUser(this.loginUserData)
  .subscribe(
    res => {
      ...
    },
    err => {
      this.flag.valid=false,
      this.message = "hello";
    }
  )
}

In your html

<span *ngIf="!flag.valid" id="login-denied">
  <i class="fa fa-times-circle" style="color:firebrick;">{{message}}</i>
</span>

Regards

Sign up to request clarification or add additional context in comments.

1 Comment

It is not recommended to access the DOM directly in angular, since your are bypassing the Angular renderer. This can produce unwanted and unpredictable results.
1

Why you need to do it with

document.querySelector

Because you can do it with a variable a variable in angular that would be more proper

 error message="";
// When error occurred set the message variable

Like this in the error function

err => {
        this.flag.valid=false;
this.errorMessage ="Some Error";
        }

And bind it in the html like this

<span *ngIf="flag.valid===false" id="login-denied">
      <i class="fa fa-times-circle" style="color:firebrick;"></i> {{errorMessage}}
  </span>

1 Comment

I saw this approach somewhere on the internet and it was working for them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.