2016-01-13 42 views
3

所以我有一個非常簡單的組件,它裝載了一個簡單的路由器。我使用的所有基本的東西,如ngFor,ngSwitch,ngIf和我通過COMMON_DIRECTIVES注入它們Angular2 EXCEPTION:沒有提供者的e! (e - > e)

我得到這個非常模棱兩可的錯誤,沒有堆棧跟蹤,所以我真的不知道發生了什麼事情。我已經在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)

這裏是我的代碼:

.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']; 
    } 
} 

編輯: 我的根.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 }) 
    ] 
); 
+2

無需添加'COMMON_DIRECTIVES'和'FORM_DIRECTIVES',它們會自動添加:你這樣引導您的應用程序時添加HTTP_PROVIDERS。所以最好刪除它們。你是否在'bootstrap'函數中添加了'HTTP_PROVIDERS'? – PierreDuc

+0

你是怎麼意思自動添加的?有沒有這方面的來源?另外檢查我的編輯 – ditoslav

+0

我無法在更新日誌中找到它,但向引導添加'FORM_PROVIDERS'也是不必要的。但是正如Thierry所說的,嘗試使用.dev.js版本來更詳細地描述你的錯誤 – PierreDuc

回答

8

要獲得更多可讀錯誤,您可以使用Angular2發行版中的xxx.dev.js文件。

您的錯誤消息表示嘗試注入某些內容時出現問題。關於您提供的代碼,它可能與Http類的實例的注入有關。

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

bootstrap(AppComponent, [ HTTP_PROVIDERS ]); 
+0

感謝.dev的建議。我有它bootstrapped壽...(看我的編輯) – ditoslav

+0

看來我失蹤 >例外:沒有供應商NgSwitch! (NgSwitchWhen - > NgSwitch),但事件後,我添加它仍然給我錯誤 – ditoslav

+0

你是否定義這個在你的組件:'指令:[NgSwitch,NgSwitchWhen,NgSwitchDefault]'。這是一個使用示例:http://plnkr.co/edit/DQMTII95CbuqWrl3lYAs?p=preview。 –

相關問題