在Simple 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>
&#13;
答案 0 :(得分:1)
添加脏标志以检查它是第一次加载还是下次加载。