Làm thế nào một người theo dõi / kích hoạt một sự kiện trên một tuyến đường thay đổi?
Làm thế nào một người theo dõi / kích hoạt một sự kiện trên một tuyến đường thay đổi?
Câu trả lời:
Lưu ý : Đây là một câu trả lời thích hợp cho phiên bản kế thừa của AngularJS. Xem câu hỏi này cho các phiên bản cập nhật.
$scope.$on('$routeChangeStart', function($event, next, current) {
// ... you could trigger something here ...
});
Các sự kiện sau đây cũng có sẵn (các hàm gọi lại của chúng có các đối số khác nhau):
Xem các tài liệu tuyến đường $ .
Có hai sự kiện không có giấy tờ khác :
Xem sự khác biệt giữa $ locationChangeSuccess và $ locationChangeStart là gì?
$locationChangeStart
và $locationChangeSuccess
bây giờ được ghi lại! docs.angularjs.org/api/ng.$location
$rootScope.$on("$routeChangeStart", function (event, next, current) {
bây giờ.
Nếu bạn không muốn đặt đồng hồ bên trong một bộ điều khiển cụ thể, bạn có thể thêm đồng hồ cho toàn bộ ứng dụng trong ứng dụng Angular run()
var myApp = angular.module('myApp', []);
myApp.run(function($rootScope) {
$rootScope.$on("$locationChangeStart", function(event, next, current) {
// handle route changes
});
});
$rootScope.$on( "$routeChangeStart", function(event, next, current) {
//..do something
//event.stopPropagation(); //if you don't want event to bubble up
});
$rootScope.$on( "$routeChangeStart", function(event, next, current) {
//if you want to interrupt going to another location.
event.preventDefault(); });
Đây là dành cho người mới bắt đầu ... như tôi:
HTML:
<ul>
<li>
<a href="#"> Home </a>
</li>
<li>
<a href="#Info"> Info </a>
</li>
</ul>
<div ng-app="myApp" ng-controller="MainCtrl">
<div ng-view>
</div>
</div>
Góc cạnh:
//Create App
var app = angular.module("myApp", ["ngRoute"]);
//Configure routes
app.config(function ($routeProvider) {
$routeProvider
.otherwise({ template: "<p>Coming soon</p>" })
.when("/", {
template: "<p>Home information</p>"
})
.when("/Info", {
template: "<p>Basic information</p>"
//templateUrl: "/content/views/Info.html"
});
});
//Controller
app.controller('MainCtrl', function ($scope, $rootScope, $location) {
$scope.location = $location.path();
$rootScope.$on('$routeChangeStart', function () {
console.log("routeChangeStart");
//Place code here:....
});
});
Hy vọng điều này sẽ giúp một người mới bắt đầu như tôi. Dưới đây là mẫu làm việc đầy đủ:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.min.js"></script>
</head>
<body>
<ul>
<li>
<a href="#"> Home </a>
</li>
<li>
<a href="#Info"> Info </a>
</li>
</ul>
<div ng-app="myApp" ng-controller="MainCtrl">
<div ng-view>
</div>
</div>
<script>
//Create App
var app = angular.module("myApp", ["ngRoute"]);
//Configure routes
app.config(function ($routeProvider) {
$routeProvider
.otherwise({ template: "<p>Coming soon</p>" })
.when("/", {
template: "<p>Home information</p>"
})
.when("/Info", {
template: "<p>Basic information</p>"
//templateUrl: "/content/views/Info.html"
});
});
//Controller
app.controller('MainCtrl', function ($scope, $rootScope, $location) {
$scope.location = $location.path();
$rootScope.$on('$routeChangeStart', function () {
console.log("routeChangeStart");
//Place code here:....
});
});
</script>
</body>
</html>