如何从另一个控制器访问另一个控制器的操作

How to access another controller's action from another controller

我刚刚将我的 Ember 应用程序升级到最新版本, 但随后在测试其功能时,某些操作不起作用。 我以前的代码在旧版本中运行良好,如下所示。

export default Ember.Controller.extend({
needs: 'sales-order',
actions: {
    showModal: function(mode){
        this.get('controllers.sales-order').send('showModal', mode);
    }
  }
});

看起来“需要”已经折旧了。

您应该使用 Ember.inject.controller 而不是 needs。它应该看起来像这样:

export default Ember.Controller.extend({
  salesOrder: Ember.inject.controller(),

  actions: {
    showModal(mode) {
      this.get('salesOrder').send('showModal', mode);
    }
  }
});

您可以在 Managing Dependencies Between Controllers 指南中找到更多信息。