Calling the controller method in another Ember controller

I am using Ember Need Api to call a controller method in another controller. I can get an instance of the controller, but when I call its method, it returns me this error TypeError: Object [object Object] has no method.

This is what I call it:

 Cards.CardsIndexController = Ember.Controller.extend({ needs: 'account_info', actions: { accountInfoStart:function(){ console.log(this.get('controllers.account_info').test()); // error here } } }); 

This is the controller whose function I want to call

 Cards.AccountInfoController = Ember.Controller.extend({ actions:{ test: function(){ alert(1); } } }); 

How can I solve it?

+7
source share
3 answers

test not technically a method, but an action or event. Use the send method instead:

 this.get('controllers.account_info').send('test', arg1, arg2); 
+12
source

According to the Ember documentation; create a property that lazily searches for another controller in the container. This can only be used when defining another controller.

Ember Application Example:

 App.PostController = Ember.Controller.extend({ accountInfo: Ember.inject.controller() this.get('accountInfo').send('test') }); 

example of the use of modern amber:

 // in an ember app created with ember-cli // below snippet would be the app/controllers/post.js file import Ember from 'ember'; export default Ember.Controller.extend({ appController: Ember.inject.controller('application') }); 

You can find more documentation on Ember.inject here

+6
source

From the updated Ember documentation:

 import { inject } from '@ember/controller'; export default Ember.Controller.extend({ appController: inject('application') }); 

For further reference, you can find out at this link https://guides.emberjs.com/release/applications/dependency-injection/#toc_ad-hoc-injection

0
source

All Articles