Ionic 어플리케이션에서 하드웨어 되돌리기 버튼을 비활성화하시겠습니까?
코르도바 앱의 뒤로 버튼을 비활성화하려고 합니다.Angular를 사용하고 있습니다.JS + 이온 프레임워크이에 대한 토픽을 찾아 아래 코드를 사용해 봤지만 전혀 효과가 없습니다.감 잡히는 게 없어요?
index.displaces를 표시합니다.
<head>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("backbutton", function (e) {
e.preventDefault();
console.log("hello");
}, false );
}
</script>
</head>
버튼을 누르면 콘솔에 "hello"가 표시됩니다.
드디어 이 Ionic Forum 스레드에서 답을 찾았습니다.
$ionicPlatform.registerBackButtonAction(function () {
if (condition) {
navigator.app.exitApp();
} else {
handle back action!
}
}, 100);
$ionicPlatform.registerBackButtonAction
를 사용하면 뒤로 버튼 동작을 완전히 덮어쓸 수 있습니다.첫 번째 파라미터는 콜백 함수이고 두 번째 파라미터는 priority가 가장 높은 콜백만 실행됩니다.
$ionicPlatform.registerBackButtonAction(function (event) {
event.preventDefault();
}, 100);
이렇게 하면 뒤로 버튼의 기능을 할 수 없게 됩니다.
David D의 답변을 확대하기 위해 되돌아가기 구현을 포함했습니다.
이 기능을 응용 프로그램에 추가.run
기능:
$ionicPlatform.registerBackButtonAction(function (event) {
if ($ionicHistory.currentStateName() === 'someStateName'){
event.preventDefault();
} else {
$ionicHistory.goBack();
}
}, 100);
이것은 컨트롤러에서는 동작하지 않습니다.어플리케이션 전체에 걸칩니다.
간단한 트릭으로 인해 한 페이지로 돌아가지 않습니다.
`.controller('DashCtrl', function($scope,$ionicHistory) {
$ionicHistory.clearCache();
$ionicHistory.clearHistory();
})`
이 문서의 예는 이벤트청취자를 나타내고 있습니다.deviceready
- 문서 뒤에 첨부됩니다.onload
이벤트가 발생하였습니다.
코드 사용:
function onDeviceReady() {
document.addEventListener("backbutton", function (e) {
e.preventDefault();
console.log("hello");
}, false);
}
document.onload = function () {
document.addEventListener("deviceready", onDeviceReady, false);
};
앱이 장치 뒤로 버튼 기능을 사용하지 못하도록 하려면
$ionicPlatform.registerBackButtonAction(function (event) {
event.preventDefault();
}, 100);
특정 페이지 사용을 금지하려면
$ionicPlatform.registerBackButtonAction(function (event) {
event.preventDefault();
if ($location.path() === "/pagename" || $location.path() === "pagename") {
navigator.app.exitApp();
} else {
$ionicHistory.goBack();
}
}, 100);
Ionic 어플리케이션에서 컨트롤러(또는 컴포넌트의 컨트롤러)의 하드웨어 백버튼을 비활성화하려면 다음 회피책을 강구할 수 있습니다.단, 실제로는 컨트롤러 자체가 아닌 컨트롤러와 스테이트의 조합으로 컨트롤러에 통상적인 코드를 추가합니다.
var deRegisterHardBack = $ionicPlatform.registerBackButtonAction(
function (event) {
if (youConditionHere) {
event.preventDefault();
// do something
} else {
$ionicHistory.goBack();
}
}, 100);
하지만 당신의 안에서$stateProvider
더하다disableHardwareBackButton
다음과 같습니다.
$stateProvider
.state('app.stateB', {
url: '/page-b/:id',
template: '<ion-view><ion-nav-title>Sub Page </ion-nav-title>Hello</ion-view>',
disableHardwareBackButton : true
});
모듈 내('app'). 실행 기능:
$ionicPlatform.registerBackButtonAction(function(event){
if ($state.current.disableHardwareBackButton){
event.preventDefault();
} else {
$ionicHistory.goBack();
}
}
이 방법으로 '서브섹션' 또는 '컨트롤러 내부'의 문제를 회피할 수 있습니다.
Ionic 3의 경우:
// root component
export class MyApp {
constructor(platform: Platform) {
platform.ready().then(() => {
platform.registerBackButtonAction(() => {
this.customBackButtonHandler();
}, 100)
});
}
customBackButtonHandler() {
...
}
}
언급URL : https://stackoverflow.com/questions/26548418/disable-hardware-back-button-in-ionic-application
'programing' 카테고리의 다른 글
리액트 및 노드에 대해 프록시가 작동하지 않음 (0) | 2023.03.13 |
---|---|
JSON에서 TypeScript 클래스 인스턴스로? (0) | 2023.03.13 |
오브젝트 JSON을 일반 인코더로 시리얼화 (0) | 2023.03.13 |
JSON 표현에서의 링크 관계 (0) | 2023.03.13 |
안심.request json에서 값을 추출할 수 있습니까? (0) | 2023.03.13 |