2015-06-06 83 views
1

我已经从angular.io开始学习Angular2。我从本教程中学习了一个示例。错误:在ES5中没有服务提供商Angular2

created plunker

组件:

function FriendService(){ 
    this.friendList = ['Wam','Pam','Sam','Cam']; 
} 

function PersonalInfo (friends){ 
    this.myName="Jam"; 

} 
PersonalInfo.annotations = [ 
    new angular.ComponentAnnotation({ 
    selector : 'app', 
    injectables :[FriendService] 
    }), 
    new angular.ViewAnnotation({ 
    template:'<h3>{{myName}} Friend\'s</h3>' 
    }) 
]; 
PersonalInfo.parameters =[[FriendService]]; 
document.addEventListener('DOMContentLoaded', function(){ 
    angular.bootstrap(PersonalInfo); 
}) 

查看:

<body> 
    <app></app> 
    </body> 

这里,我已经注入FriendService的成分,也传递参数给PersonalInfo组件。但它给错误:

没有FriendService的提供商! (PersonalInfo - > FriendService)

有人对这个错误有什么想法吗?

回答

0

从Angular 2 alpha-25 +,Component.injectables已成为Component.appInjector

另外,我不确定参数是否需要双方括号。虽然我可能是错的。您可能要考虑使用A library在Angular中编写ES5注释。

尝试这些变化:

PersonalInfo.annotations = [ 
    new angular.ComponentAnnotation({ 
    selector : 'app', 
    appInjector :[FriendService] // injectables -> appInjector 
    }), 
    new angular.ViewAnnotation({ 
    template:'<h3>{{myName}} Friend\'s</h3>' 
    }) 
]; 
PersonalInfo.parameters =[FriendService]; // [[]] -> [] 
1

更新2016年1月14日 - [email protected]^2.0.0-beta.1

这是新的语法。

function FriendService(){ 
    this.friendList = ['Wam','Pam','Sam','Cam']; 
} 

var PersonalInfo = ng.core 
    .Component({ 
    selector: 'app', 
    providers: [FriendService] 
    }) 
    .View({ 
    template:'<h3>{{myName}} Friend\'s</h3>' 
    }) 
    .Class({ 
    constructor: [FriendService, function(friends){ 
     this.myName="Jam"; 
    }] 
    }) 

ng.platform.browser.bootstrap(PersonalInfo) 
相关问题