2016-10-24 2 views
1

창이 닫히기 전에 일부 코드를 실행하고 싶지만 프롬프트를 표시하고 싶지 않습니다. 내 코드가 실행하기되지 않는 정의되지 않았거나 거짓이 작동하지 않습니다 반환Angularjs 프롬프트가 표시되지 않고 켜기가 제대로 작동하지 않습니다.

$window.onbeforeunload = function(){ 
     $rootScope.$broadcast('war-change'); 
     return undefined; 
    }; 

    $scope.$on('war-change',function(){ 
     console.log('here'); 
     //Execute some more code here. 
     }); 

: 다음은 내 코드입니다. 도움을 주시면 감사하겠습니다.

답변

0

코드는 의도 한대로 작동합니다. 한 번이다 언로드 때문에, 당신은 beforeunload 방법으로 할 수있는 일에 한계가있다,

function unloadCtrl($scope,$window,$rootScope) { 
    $window.onbeforeunload = function(){ 
     $rootScope.$broadcast('war-change'); 
     return undefined; 
    }; 

    $scope.$on('war-change',function(){ 
     console.log('here'); 
     //Execute some more code here. 
     }); 
} 

을하지만 : 당신이 가지고있는 같은

https://jsfiddle.net/8ubz4bxg/

<div ng-app> 
    <h2>Test unload</h2> 
    <div ng-controller="unloadCtrl"> 
    </div> 
</div> 

그리고 컨트롤러 : 다음 바이올린을 확인 당신은 무언가를 할 아주 제한된 시간을 보냈습니다. 당신이 얼마나 많은 시간을 테스트하려면 다음과 같은 수정 사용할 수 있습니다 : 당신은 0 ~ 100 같은 시간 초과를 줄이면 지금

$scope.$on('war-change',function(){ 
    console.log('here'); 
    //Execute some more code here. 
    setTimeout(function(){ console.log("ha-ha")}, 100) 
    console.log("he-he") 
    }); 

을, 당신은 여전히 ​​그가 출력하는 int "하 - 하"를 볼 수 있습니다. 2 초 (2000)로 증가 시키면 그 행은 보이지 않을 것입니다.