2015-06-06 78 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) 
相關問題