组件中的appInjector无法正常工作

时间:2015-07-20 07:43:14

标签: dependency-injection angular

我仍在努力使用角度2.我正在尝试使用http模块,但我得到了未解决的依赖关系。我正在使用alpha 31.这是我的组件的来源:

/// <reference path="../typings/angular2/angular2.d.ts" />

import {Component, View} from 'angular2/angular2';
import {Http, httpInjectables} from 'angular2/angular2';

// Annotation section
@Component({
    selector: 'test',
    appInjector: [httpInjectables]
})
@View({
    templateUrl: 'EcoApp/Views/test.html'
})
// Component controller
export class Test2Component {
    name: string;

    constructor(http: Http) {
        this.name = http.toString();
    }
}

这是我遇到的第一个错误(还有其他错误,未在此处列出):

No provider for function (_backend, _defaultOptions) {
        this._backend = _backend;
        this._defaultOptions = _defaultOptions;
      }! (Test2Component -> function (_backend, _defaultOptions) {
        this._backend = _backend;
        this._defaultOptions = _defaultOptions;
      })
STACKTRACE:
Error: null
    at NoBindingError.BaseException (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:7713:25)
    at NoBindingError.AbstractBindingError (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:9595:66)
    at new NoBindingError (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:9613:60)
    at Injector._getByKey (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:27021:21)
    at Injector._getByDependency (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:26979:27)
    at Injector._new (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:26877:68)
    at InjectorInlineStrategy.hydrate (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:26618:31)
    at ElementInjector._reattachInjector (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:28293:39)
    at ElementInjector._hydrateInjector (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:28279:20)
    at ElementInjector.hydrate (https://code.angularjs.org/2.0.0-alpha.31/angular2.dev.js:28263:18)
    BrowserDomAdapter.logError @ angular2.dev.js:20567
    ExceptionHandler.call @ angular2.dev.js:13852
    _errorHandler @ angular2.dev.js:14140
    NgZone._onError @ angular2.dev.js:14079
    errorHandling.onError @ angular2.dev.js:14022
    run @ angular2.dev.js:136
    (anonymous function) @ angular2.dev.js:14036
    zoneBoundFn @ angular2.dev.js:106
    lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1356
    lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1368
    lib$es6$promise$$internal$$publish @ angular2.dev.js:1339
    (anonymous function) @ angular2.dev.js:184
    microtask @ angular2.dev.js:14061
    run @ angular2.dev.js:133
    (anonymous function) @ angular2.dev.js:14036
    zoneBoundFn @ angular2.dev.js:106
    lib$es6$promise$asap$$flush @ angular2.dev.js:1150

1 个答案:

答案 0 :(得分:3)

好的,我必须根据@jhadesdev的回答中的链接回答我自己发现的问题。似乎appInjector已重命名为viewInjector,当我使用后者时它工作得很好。

这里是该问题的链接和揭示帖子

https://github.com/angular/angular/issues/2788#issuecomment-117968971

谢谢大家