我需要覆盖两个按钮的后退按钮功能:
但仅针对一个特定视图,而不是全局视图。我怎么能这样做?
答案 0 :(得分:4)
可以从控制器中覆盖两个按钮的后退按钮功能。这是代码:
// run this function when either hard or soft back button is pressed
var doCustomBack = function() {
console.log("custom BACK");
};
// override soft back
// framework calls $rootScope.$ionicGoBack when soft back button is pressed
var oldSoftBack = $rootScope.$ionicGoBack;
$rootScope.$ionicGoBack = function() {
doCustomBack();
};
var deregisterSoftBack = function() {
$rootScope.$ionicGoBack = oldSoftBack;
};
// override hard back
// registerBackButtonAction() returns a function which can be used to deregister it
var deregisterHardBack = $ionicPlatform.registerBackButtonAction(
doCustomBack, 101
);
// cancel custom back behaviour
$scope.$on('$destroy', function() {
deregisterHardBack();
deregisterSoftBack();
});
确保将$rootScope
注入控制器。
有关详细信息和正确解释,请参阅相关问题的完整答案:
答案 1 :(得分:2)
此代码用于Android按钮,而导航栏上的按钮更简单:
Android按钮:
$ionicPlatform.registerBackButtonAction(function (event) {
if($state.current.name=="home"){
alert("button back");
}
}, 100);
离子按钮:
您可以修改主题,看看如何定义菜单和观点?