Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 EXCEPTION: No provider for e! (e -> e)

Tags:

angular

So I have a pretty simple component which gets loaded with a simple router. I'm using all the basic stuff like ngFor, ngSwitch, ngIf and I'm injecting them via COMMON_DIRECTIVES

I get this pretty ambiguous error with no stack trace so I really don't know what's going on. I have seen something potentionally similar in https://github.com/angular/angular/issues/6223 .

EXCEPTION: No provider for e! (e -> e)

STACKTRACE:

Error: DI Exception at t [as constructor] (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:6:5082) at t [as constructor] (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:1:26551) at new t (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:1:27079) at e._throwOrNull (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:5943) at e._getPrivateDependency (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:6605) at e._getByKeyHost (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:6397) at e._getByKey (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:5826) at e._getByDependency (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:5602) at e._instantiate (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:3644) at e._instantiateProvider (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:3376)

Here's my code:

.jade:

.center-area('data-editable'="true")
  header.center-header
    .view-path Upravljaj administratorima
  .center-content
    header.content-header
      .content-header-left
        .content-header-title Administratori
        .content-header-subtitle
          | Pregledajte i upravljajte administratorima
          | radiopostaje – dodijelite administrativne
          | ovlasti korisnicima ili ih uklonite postojećim administratorima. 
          b Sustav smije imati najviše 10 administratora.
      .content-header-right
        i.material-icons.icon edit
    nav.content-options( '[ngSwitch]'="editable" '[ngClass]'="{'uneditable-ui': !editable, 'editable-ui': editable}" )
      button( '*ngSwitchWhen'="false" '(click)'="toggleEditable()" ) Uredi popis administratora
    nav.content-options.editable-ui
      button( '*ngSwitchWhen'="true" '(click)'="toggleEditable()" ) Završi uređivanje popisa
    ul.content-primary.content-list
      li.content-list-item.admin-item( '*ngFor'="#admin of admins")
        .content-list-item-content
          .content-list-item-name {{admin.first_name}} {{admin.last_name}}
          .content-list-item-data
            .content-list-item-data-item.user-mail {{admin.email}}
            .content-list-item-data-item.user-age {{admin.year_of_birth}}
            .content-list-item-data-item.user-occupation {{admin.occupation}}
        .content-list-item-options.editable-ui
          button.alt
            i.material-icons clear
      li.content-list-item.content-list-search-item.editable-ui( '*ngIf'="editable" )
        form
          .content-list-item-content
            label(for='add-item-search') Dodaj administratora
            input.add-item-search(type='text', name='add_track_search')
          .content-list-item-options
            button.raised.add-item-button
              i.material-icons person_add

.ts:

import { View, Component } from 'angular2/core';
import { Location, RouteConfig, RouterLink, Router, CanActivate } from 'angular2/router';
import { Http } from 'angular2/http';
import { COMMON_DIRECTIVES, FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators, Control } from 'angular2/common';

import { Form } from '../../utilities';

@Component({
    selector: 'ManageAdmins',
    templateUrl: './dest/settings/manageAdmins/manageAdmins.html',
    directives: [COMMON_DIRECTIVES, FORM_DIRECTIVES]
})
export class ManageAdmins {
    admins: Admin[];

    editable: boolean;

    toggleEditable() {
        this.editable = !this.editable;
    }

    constructor(http: Http) {
        this.editable = false;
        this.admins = new Array();
        http.get('/owner/admins/list').map((res) => res.json().data).subscribe((res) => {
            for (let i in res) {
                let admin = new Admin(res[i]);
                this.admins.push(admin);
            }
        });
    }
}

class Admin {
    id: number;
    first_name: string;
    last_name: string;
    email: string;
    year_of_birth: string;
    occupation: string;

    constructor(obj: Object) {
        this.id = obj['id'];
        this.first_name = obj['first_name'];
        this.last_name = obj['last_name'];
        this.email = obj['email'];
        this.year_of_birth = obj['year_of_birth'];
        this.occupation = obj['occupation'];
    }
}

EDIT: my root .ts

import { FORM_PROVIDERS } from 'angular2/common';
import { ROUTER_PROVIDERS, Location, LocationStrategy, PathLocationStrategy } from 'angular2/router';
import { HTTP_PROVIDERS, RequestOptions, BaseRequestOptions } from 'angular2/http';
import { bootstrap } from 'angular2/platform/browser';
import { provide, Injectable } from 'angular2/core';

import { App } from './app/app';

@Injectable()
export class DefaultRequestOptions extends BaseRequestOptions {
    constructor() {
        super();
        this.headers.set("Content-Type", "application/x-www-form-urlencoded");
    }
}

bootstrap(
    App,
    [
        FORM_PROVIDERS,
        ROUTER_PROVIDERS,
        HTTP_PROVIDERS,
        provide(LocationStrategy, { useClass: PathLocationStrategy }),
        provide(RequestOptions, { useClass: DefaultRequestOptions })
    ]
);
like image 544
ditoslav Avatar asked Feb 09 '23 01:02

ditoslav


1 Answers

To have more readable errors, you could use the xxx.dev.js file from the Angular2 distribution.

Your error message said that there is a problem when trying to inject something. Regarding the code you provide, it could be related to the injection of the instance of the Http class. Do you add the HTTP_PROVIDERS when bootstrapping your application:

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';
import {HTTP_PROVIDERS} from 'angular2/http';

bootstrap(AppComponent, [ HTTP_PROVIDERS ]);
like image 174
Thierry Templier Avatar answered Feb 11 '23 16:02

Thierry Templier