admin管理员组文章数量:1418346
I am using the Javascript command: setInterval. I like to stop it when the user leaves the page.
This code seems to work well: /
It detects when a user leaves a page. It executes Javascript code when a user clicks on a link to go to a different HTML page or URL, or if user reloads page.
However, it does not work when I go from one AngularJS template to another. As an example, if I am at template1.html, I want the Javascript code to do something in Controller1.js when the user leaves template1.html to go to template2.html. What is the equivalent of this code below in AngularJS?:
$(window).on('beforeunload', function() {
return 'Your own message goes here...';
});
I am using the Javascript command: setInterval. I like to stop it when the user leaves the page.
This code seems to work well: http://jsfiddle.net/PQz5k/
It detects when a user leaves a page. It executes Javascript code when a user clicks on a link to go to a different HTML page or URL, or if user reloads page.
However, it does not work when I go from one AngularJS template to another. As an example, if I am at template1.html, I want the Javascript code to do something in Controller1.js when the user leaves template1.html to go to template2.html. What is the equivalent of this code below in AngularJS?:
$(window).on('beforeunload', function() {
return 'Your own message goes here...';
});
Share
Improve this question
edited Apr 8, 2014 at 18:33
tshepang
12.4k25 gold badges95 silver badges139 bronze badges
asked Dec 14, 2012 at 20:23
CurtCurt
1,1814 gold badges19 silver badges29 bronze badges
1
- Are you actually switching full pages at the html root or are you switching templates in an ng-view? – Brad Green Commented Dec 17, 2012 at 5:14
4 Answers
Reset to default 142I think you have two controllers, one for each template like this:
function Controller_1($scope...){
...
}
function Controller_2($scope...){
...
}
Well, when you switch from one template to another there's an event that's fired called $destroy, you can read up on it here http://docs.angularjs.org/api/ng.$rootScope.Scope#$destroy
Let's say I'm switching from the template with Controller_1 to the template with Controller_2. Controller_1 has an interval I'd like to stop. You can accomplish this with:
function Controller_1($scope, $interval...){
var myInterval = $interval(...);
$scope.$on("$destroy", function(){
$interval.cancel(myInterval);
});
}
This will mean that when the $scope for Controller_1 is destroyed, the event will be called and the interval will be cleared.
This is for when you leave a template (also prompt a confirm dialog):
function Controller_1($scope...){
var myInterval = setInterval(...);
$scope.$on('$locationChangeStart', function (event, next, current) {
console.log(current);
if (current.match("\/yourCurrentRoute")) {
var answer = confirm("Are you sure you want to leave this page?");
if (!answer) {
event.preventDefault();
}else{
clearInterval(myInterval);
}
}
});
}
if you are using ui-router then you can use the onExit, property
$stateProvider.state('foo', {
url: '/foo',
templateUrl: 'views/foo.html',
controller: 'fooController',
onExit: ['$fooService', function($fooService) => {
$fooService.hide();//do what u want to do here
}]
});
You can use a watcher to check when the location path is changed, something like this:
$scope.$watch(function(){
return $location.path();
}, function(newPath, oldPath){
//...Do something
})
Then, you can get the old location, and the new location and execute a function or whatever you want,
本文标签: javascriptIn AngularJShow to detect when user leaves templatepageStack Overflow
版权声明:本文标题:javascript - In AngularJS, how to detect when user leaves templatepage? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736997509a1959014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论