2015-08-27 71 views
3

有關於離子覆蓋的實體Android後退按鈕的問題,爲客戶提供定製的行爲:離子取消硬BACK按鈕覆蓋

但你如何取消覆蓋恢復默認行爲?


我已經嘗試更改處理程序的優先級,希望默認處理程序可能具有更高的優先級。

var customBackButton = function() { 
    console.log("this is custom behaviour"); 
}; 
$ionicPlatform.registerBackButtonAction(
    customBackButton, 101 
); 
$scope.$on('$destroy', function() { 
    $ionicPlatform.registerBackButtonAction(
     customBackButton, 0 
    ); 
}); 

這不起作用。


回答

2

離子V1溶液(過時)


根據該Ionic docs for $ionicPlatform,所述registerBackButtonAction()返回:

的函數,調用它時,將註銷該backButtonAction 。

這可以在代碼中看到了registerBackButtonAction()

// return a function to de-register this back button action 
    return function() { 
    delete self. [action.id]; 
    }; 

所以正確的方式註銷/取消定製行爲是調用該函數當控制器被破壞:

var customBackButton = function() { 
    console.log("this is custom behaviour"); 
}; 

// registerBackButtonAction() returns a function which can be used to deregister it 
var deregisterBackButtonAction = $ionicPlatform.registerBackButtonAction(
    customBackButton, 101 
); 

$scope.$on('$destroy', function() { 
    deregisterBackButtonAction(); 
}); 

一個更完整的例子展示如何覆蓋&恢復軟硬按鈕可以在這裏找到:

+1

爲了完整 - http://ionicframework.com/docs/api/directive/ionView/ - *瀏覽可以被緩存,這意味着控制器通常只加載一次,這可能會影響你的控制器邏輯。* - 註銷''ionicView.leave'''可能比''''destroy'''更好的選擇 –

+0

@Michal看起來更好的選擇。當視圖回到......錯誤...進入視野時,當然必須在適當的事件中重新註冊它。 –

+0

你可以更新你的答案離子3 –