我有一个应用程序,它有一个我放入的导航栏"应用程序"上下文。默认情况下,这些导航栏链接将被禁用,并且仅对模板中的特定操作启用。
应用程序控制器,其中包含导航栏中链接的禁用状态: -
App.ApplicationController = Ember.Controller.extend({
userName:"TestUser",
firstLinkDisabled:true,
actions:{
handleToggle:function(){
console.log("Handling application action with linkstatus="+this.firstLinkDisabled);
//this.set("firstLinkDisabled",false);
this.toggleProperty("firstLinkDisabled");
}
}
})
将操作发送到应用程序控制器的索引控制器: -
App.IndexController = Ember.Controller.extend({
actions:{
toggleApplicationButton:function(){
this.controllerFor("Application").send("handleToggle");
}
}
})
申请模板:
<script type="text/x-handlebars">{{#link-to 'first' disabled=firstLinkDisabled}}First link in application{{/link-to}}
{{outlet}} </script>
<button {{action 'handleToggle'}}>Toggle Application Menu </button>
索引模板
<script type="text/x-handlebars" id="index"> <button {{action 'toggleApplicationButton'}}>Toggle Application Menu </button> </script>
当我点击&#34;切换应用程序菜单&#34;按钮我在控制台中获得以下输出。
但在Ember督察中,该物业&#34; firstLinkDisabled&#34;不会改变。 Ember Inspector的图片: - Ember Inspector Image
该链接仍然无效。
我在这里做错了什么?
不允许改变其他控制器的属性吗?
如何让这件事有用?
答案 0 :(得分:1)
以下是我项目中的一个简单示例:
import Ember from 'ember';
export default Ember.Controller.extend({
/*first, inject a controller*/
loginController: Ember.inject.controller('lang.login'),
/*some code*/
actions: {
register: function () {
/*some code*/
/*work with injected controller*/
var c = this.get('loginController');
c.set('identification', that.get('user').email);
c.set('password', that.get('user').plainPassword);
/*some code*/
}
}
});
答案 1 :(得分:1)
使用Ember.inject.controller()
App.IndexController = Ember.Controller.extend({
appController: Ember.inject.controller("Application"),
actions:{
toggleApplicationButton:function(){
this.get("appController").send('handleToggle');
}
}
})
您可以在路线中使用 controllerFor
您也可以使用alias
appController: Ember.computed.alias('controllers.Application')
请参阅Emberjs official documentation了解详情
答案 2 :(得分:0)
首先,将应用程序控制器注入索引控制器
//index.js
application: Ember.inject.controller(),
然后您可以像这样访问应用程序控制器中的属性:
//index.js
application.toggleProperty("firstLinkDisabled");