Ember.js How to get controller in needs which is nested controllerName

后端 未结 3 1431
感情败类
感情败类 2021-01-04 03:06

I want to use this.get(\'controllers.pack.query\'); to get App.PackQueryController in App.PackController, but failed.

I think

3条回答
  •  旧时难觅i
    2021-01-04 03:15

    Ember.inject.controller() should be used to access a controller. Use it like so:

    Setting

    ...
    myController: Ember.inject.controller('pack'),
    nestedController: Ember.inject.controller('pack/query')
    ...
    

    Getting

    ...
    this.get('myController');
    this.get('nestedController');
    ...
    

    The answer above was updated to reflect the needs deprecation in Ember 1.13.5 (released July 19, 2015). I've left the old answers below, but shouldn't be used unless you're using an older version of Ember.


    [DEPRECATED] Accessing nested controllers from other controllers using needs:

    Set needs on the controller:

    ...
    needs: ['pack/query'],
    ...
    

    Then access it using:

    this.get('controllers.pack/query');
    

    [DEPRECATED] Accessing nested controllers from routes:

    Ideally, actions should be put on a Route. If you're using the needs pattern outlined above in your actions on a controller, consider refactoring.

    You can access nested controllers from a Route using controllerFor like so:

    this.controllerFor('pack/query')
    

提交回复
热议问题