简体   繁体   中英

Error : No provider for service in ES5 Angular2

I have started learning Angular2 from angular.io . i have taken an example from this tutorial.

created plunker

Component :

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);
})

View :

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

here i have injected FriendService to the component and also passed parameter to to PersonalInfo component. but it is giving error :

No provider for FriendService! (PersonalInfo -> FriendService)

anyone have any idea about this error ?

Update January 14, 2016 - angular2@^2.0.0-beta.1

This is the new syntax.

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)

From Angular 2 alpha-25+, Component.injectables has become Component.appInjector .

Also, I'm not certain that parameters require double square brackets. Though I could be wrong. You might want to consider using the A library for writing ES5 annotations in Angular.

Try these changes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM