gpt4 book ai didi

javascript - AngularJS $locationChangeSuccess 事件在页面初始加载时触发

转载 作者:行者123 更新时间:2023-12-03 04:48:39 26 4
gpt4 key购买 nike

在简单的 AngularJS 页面中,$locationChangeSuccess 在页面初始加载时被触发。

如果在初始加载时未触发该事件,这可能会很有用。

有人知道为什么此事件 $locationChangeSuccess 在初始加载中触发的原因

var myApp = angular.module('myApp', []);

myApp.controller('myCtrl', ['$scope', '$location', '$rootScope', function($scope, $location, $rootScope){



console.log("My Controller");

$rootScope.$on('$locationChangeSuccess', function(event, newUrl, oldUrl, newState) {
console.log("route Changed");
console.log(event);
console.log(newUrl);
console.log(oldUrl);
console.log(newState);
});
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl, newState) {
console.log("route change started");
});
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>HTML BolierPlate</title>
<link rel="stylesheet" type="text/css" href="/lib/reset.css"></link>
<link rel="stylesheet" type="text/css" href="css/mystyle.css"></link>
<script src="/lib/jquery.min.js" ></script>
<script src="/lib/jquery.min.js" ></script>
<script src="bower_components/angular/angular.js"></script>

<script src="js/myscript.js" ></script>

<script>
jQuery(function($){



});

</script>
<style>
.font32{font-size:32px}

</style>

</head>

<body data-ng-app="myApp" data-ng-controller="myCtrl">

Welcome to BoilerPlate!!!

</body>
</html>

最佳答案

添加脏标志以检查是第一次加载还是下次加载。

关于javascript - AngularJS $locationChangeSuccess 事件在页面初始加载时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42757766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com