Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular:如何使用appInject将服务注入组件?_Angular - Fatal编程技术网

Angular:如何使用appInject将服务注入组件?

Angular:如何使用appInject将服务注入组件?,angular,Angular,我在关注谷歌(这些例子没有Github?!) 在注入服务之前,一切都很顺利 这是我的代码: import {Component, View, bootstrap, NgFor} from 'angular2/angular2'; class FriendsService { names: Array<string>; constructor(){ this.names = ['Aviad', 'Chen', 'Yarden']; } }

我在关注谷歌(这些例子没有Github?!)

在注入服务之前,一切都很顺利

这是我的代码:

import {Component, View, bootstrap, NgFor} from 'angular2/angular2';

class FriendsService {
    names: Array<string>;

    constructor(){
        this.names = ['Aviad', 'Chen', 'Yarden'];
    }
}

@Component({
    selector: 'display',
    appInjector: [FriendsService]
})
@View({
    template:
    <p>My name: {{ myName }}</p>
    <p>Friends:<p>
    <ul>
        <li *ng-for="#name of names">
            {{name}}
        </li>
    </ul>
    ,
    directives: [NgFor]
})

export default class DisplayComponent {
    myName: string;
    names: Array<string>;

    constructor(friendsService: FriendsService){
        this.myName = 'Alice';
        this.names = friendsService.names;
    }
}
一件可能相关的事情是
组件是
组件的指令:

import {Component, View, bootstrap} from 'angular2/angular2';
import DisplayComponent from './show-properties';

@Component({
    selector: 'my-app'
})
@View({
    template: '<display>',
    directives: [DisplayComponent]
})
class AppComponent { }

bootstrap(AppComponent);
从'angular2/angular2'导入{组件、视图、引导程序};
从“/show properties”导入DisplayComponent;
@组成部分({
选择器:“我的应用程序”
})
@看法({
模板:“”,
指令:[显示组件]
})
类AppComponent{}
bootstrap(AppComponent);
这就成功了:

@Component({
    selector: 'display',
    bindings: [FriendsService] // instead of appInjector
})
当其他任何事情都和上面一样的时候

不管怎么说,根据调查,这是一个突破性的变化(谢谢):

中断更改

appInjector属性已被删除。而是使用查看Injector主机Injector

我想Angular2快速入门指南并不像我想的那样是最新的


我正要发布一个答案。我建议你读这本书
appInjector
已在
alpha-29
中删除,当前为
alpha-37
。如您所见,它们远远落后于文档;)。(开发人员已经意识到这一点,并正在努力改进)我希望他们能相应地更新它;)但是你是对的,谢谢你的提示-我现在肯定会用它。
@Component({
    selector: 'display',
    bindings: [FriendsService] // instead of appInjector
})