Ember.js: data-binding between controllers

后端 未结 2 908
醉酒成梦
醉酒成梦 2021-01-13 02:02

I\'m trying to bind in the ApplicationController to the IndexController. Here is my jsfiddle. To summarize, here is the binding I have in the Application controller



        
2条回答
  •  感情败类
    2021-01-13 02:39

    You could also use following syntax to access other controller's properties:

    import Ember from 'ember';
    
    export default Ember.Controller.extend({
      index: Ember.inject.controller('index'),
      indexIsClicked: Ember.computed.alias("index.isClicked"),
    
      isIndexClicked: Ember.observer('index.isClicked', function() {
        alert("its changed to " + this.get("indexIsClicked"));
      })
    });
    

    However you need to make Ember initialize this controller first, so you actually need to call some computed property which depends on index (it will lazily initialize it). So, for example in template I use:

    Is index clicked: {{indexIsClicked}}
    

    Working demo.

提交回复
热议问题