Inject a service into an Ember Object [not an Ember Controller]

后端 未结 2 818
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 00:21

I\'m trying to inject an Ember service into an Ember Object but keep getting the following error:

\"Assertion Failed: Attempting to lookup an injected property o         


        
2条回答
  •  感情败类
    2021-02-14 00:28

    Well you need to passing in the container instance when you create a instance of your model. The container is accessible in the route, controllers, components with this.get('controller'). AFAIK basically anything created with the container gets the container property set. Thats why service injections work in controllers etc..

    So if you are creating the model in a route's method. The code will look like below

    App.IndexRoute = Ember.Route.extend({
      model: function() {
        var newModel = Model.create({
          container: this.get('container')
        });    
        return newModel.get('test').getText();
      }
    });
    

    Here is a working demo.

提交回复
热议问题