2016-03-30 97 views
1

我有一個應用程序具有一個導航欄,我已將其放在「應用程序」上下文中。默認情況下,這些導航欄鏈接將被禁用,並且僅在來自模板的特定操作時纔會啓用。從另一個控制器更改控制器屬性

應用控制器包含在導航欄鏈接的禁用狀態: -

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}}
<button {{action 'handleToggle'}}>Toggle Application Menu </button>

{{outlet}} </script>

索引模板

 

<script type="text/x-handlebars" id="index"> 
<button {{action 'toggleApplicationButton'}}>Toggle Application Menu </button> 
</script> 

當我點擊 「切換應用程序菜單」 按鈕,我得到的控制檯輸出以下內容。

Console Output

但在灰燼檢查屬性 「firstLinkDisabled」 不會改變。 Ember Inspector的圖片: - Ember Inspector Image

該鏈接保持禁用狀態。

我在做什麼錯在這裏?
不允許更改其他控制器的屬性嗎?
如何讓這件事情起作用?

+0

。 inject.controller()的問題是一樣的。我可以看到控制檯中的變量變化。但在** Ember檢查器中,變量保持相同**,並且鏈接仍處於禁用狀態。這是Ember中的錯誤嗎? –

回答

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*/ 
    } 
    } 
}); 

ember docs

0

首先,注入應用控制器到索引控制器

//index.js 
application: Ember.inject.controller(), 

然後你就可以在訪問屬性像這樣的應用控制器:

//index.js 
application.toggleProperty("firstLinkDisabled"); 
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查看詳細

相關問題