Comment regarder / déclencher un événement sur un changement d'itinéraire?
Comment regarder / déclencher un événement sur un changement d'itinéraire?
Réponses:
Remarque : Il s'agit d'une bonne réponse pour une version héritée d'AngularJS. Voir cette question pour les versions mises à jour.
$scope.$on('$routeChangeStart', function($event, next, current) {
// ... you could trigger something here ...
});
Les événements suivants sont également disponibles (leurs fonctions de rappel prennent différents arguments):
Voir les documents $ route .
Il existe deux autres événements non documentés :
Voir Quelle est la différence entre $ locationChangeSuccess et $ locationChangeStart?
$locationChangeStart
et $locationChangeSuccess
sont désormais documentés! docs.angularjs.org/api/ng.$location
$rootScope.$on("$routeChangeStart", function (event, next, current) {
maintenant.
Si vous ne souhaitez pas placer la montre dans un contrôleur spécifique, vous pouvez ajouter la montre pour toute l'application dans l'application 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(); });
C'est pour le débutant total ... comme moi:
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>
Angulaire:
//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:....
});
});
J'espère que cela aide un débutant total comme moi. Voici l'exemple de travail complet:
<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>