Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 error - No provider error for my login component

No provider error with Angular2.

Exact error message from browser console: "EXCEPTION: No provider for User! (Login -> User)".

The typescript compiler compiles fine.

The template loads fine.

But the browser console has that error.

Also, can you check if my template is correct with the click and ng-model.

This is my code:

 import {Component, View, Injectable, CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/angular2';
 import {AuthService} from '../../authService';

 @Injectable()
 class User{
    email:string;
    password: string;
 } 

 @Component({
   selector: 'Login',  
 })

 @View({
   templateUrl: '/src/app/components/login/login.html',
   directives: [CORE_DIRECTIVES, FORM_DIRECTIVES],
 })

 @Injectable()
 export class Login {
   authService:AuthService;
   user: User;

   constructor(authService: AuthService, user: User){   
       this.authService = authService;
       this.user = user;
   }

   login = () => {
      console.log("login");
   }
 }
enter code here

This is my template for this component:

  <section class="container centered">
      <div class="row">
          <div class="well bs-component col-xs-12 col-xs-offset-0 col-sm-12 col-sm-offset-0 col-md-6 col-md-offset-3 col-lg-offset-4 col-lg-4">
              <h3>Log In</h3>

            <form class="padding-top-20" role="form" novalidate> 
                  <div class="form-group">
                      <input class="form-control" type="text" name="email" id="email" placeholder="Email Address" [(ng-model)]="user.userName" required autofocus />
                  </div>

                  <div class="form-group">
                      <input class="form-control" type="password" name="password" id="password" placeholder="Password" [(ng-model)]="user.password" required autofocus />
                  </div>

                  <button class="btn btn-primary btn-wide" type="submit" (click)="login()">Log In</button>

                  <div class="padding-top-20 padding-bottom-20">
                      <a href="/forgotpassword" title="Forgot Password?">Forgot Password?</a>
                  </div>
              </form>
          </div>
      </div>
  </section>

Here is a screenshot of the browser console error:

enter image description here

like image 264
AngularM Avatar asked Feb 09 '23 08:02

AngularM


1 Answers

To register injectables, in your component definition, use:

@Component({
   selector: 'Login',  
   providers: [Login, User]
})
like image 152
Joy Avatar answered Feb 10 '23 20:02

Joy