AngularJS
Routing mit ngRoute
Suche…
Bemerkungen
Das ngRoute ist ein ngRoute Modul, das Routing- und Deeplinking-Dienste und Anweisungen für eckige Apps bietet.
Die vollständige Dokumentation zu ngRoute Sie unter https://docs.angularjs.org/api/ngRoute
Grundlegendes Beispiel
In diesem Beispiel wird das Einrichten einer kleinen Anwendung mit 3 Routen mit jeweils eigener Ansicht und Controller unter Verwendung der controllerAs Syntax veranschaulicht.
Wir konfigurieren unseren Router mit der Funktion .config
- Wir injizieren
$routeProviderin.config - Wir definieren unsere
.whenbei der Methode.whenmit einem.when. - Wir liefern die
.whenMethode mit einem Objekt, das unseretemplateodertemplateUrl,controllerundcontrollerAsangibt
app.js
angular.module('myApp', ['ngRoute'])
.controller('controllerOne', function() {
this.message = 'Hello world from Controller One!';
})
.controller('controllerTwo', function() {
this.message = 'Hello world from Controller Two!';
})
.controller('controllerThree', function() {
this.message = 'Hello world from Controller Three!';
})
.config(function($routeProvider) {
$routeProvider
.when('/one', {
templateUrl: 'view-one.html',
controller: 'controllerOne',
controllerAs: 'ctrlOne'
})
.when('/two', {
templateUrl: 'view-two.html',
controller: 'controllerTwo',
controllerAs: 'ctrlTwo'
})
.when('/three', {
templateUrl: 'view-three.html',
controller: 'controllerThree',
controllerAs: 'ctrlThree'
})
// redirect to here if no other routes match
.otherwise({
redirectTo: '/one'
});
});
Dann definieren wir in unserem HTML- helloRoute unsere Navigation mithilfe von <a> Elementen mit href . Für einen helloRoute von helloRoute wir als <a href="#/helloRoute">My route</a>
Wir bieten unserer Ansicht auch einen Container und die Direktive ng-view , um unsere Routen einzuschleusen.
index.html
<div ng-app="myApp">
<nav>
<!-- links to switch routes -->
<a href="#/one">View One</a>
<a href="#/two">View Two</a>
<a href="#/three">View Three</a>
</nav>
<!-- views will be injected here -->
<div ng-view></div>
<!-- templates can live in normal html files -->
<script type="text/ng-template" id="view-one.html">
<h1>{{ctrlOne.message}}</h1>
</script>
<script type="text/ng-template" id="view-two.html">
<h1>{{ctrlTwo.message}}</h1>
</script>
<script type="text/ng-template" id="view-three.html">
<h1>{{ctrlThree.message}}</h1>
</script>
</div>
Beispiel für Routenparameter
Dieses Beispiel erweitert das grundlegende Beispiel, indem Parameter in der Route übergeben werden, um sie in der Steuerung zu verwenden
Dazu müssen wir:
- Konfigurieren Sie die Position und den Namen des Parameters im Routennamen
-
$routeParamsDienst in unserem Controller
app.js
angular.module('myApp', ['ngRoute'])
.controller('controllerOne', function() {
this.message = 'Hello world from Controller One!';
})
.controller('controllerTwo', function() {
this.message = 'Hello world from Controller Two!';
})
.controller('controllerThree', ['$routeParams', function($routeParams) {
var routeParam = $routeParams.paramName
if ($routeParams.message) {
// If a param called 'message' exists, we show it's value as the message
this.message = $routeParams.message;
} else {
// If it doesn't exist, we show a default message
this.message = 'Hello world from Controller Three!';
}
}])
.config(function($routeProvider) {
$routeProvider
.when('/one', {
templateUrl: 'view-one.html',
controller: 'controllerOne',
controllerAs: 'ctrlOne'
})
.when('/two', {
templateUrl: 'view-two.html',
controller: 'controllerTwo',
controllerAs: 'ctrlTwo'
})
.when('/three', {
templateUrl: 'view-three.html',
controller: 'controllerThree',
controllerAs: 'ctrlThree'
})
.when('/three/:message', { // We will pass a param called 'message' with this route
templateUrl: 'view-three.html',
controller: 'controllerThree',
controllerAs: 'ctrlThree'
})
// redirect to here if no other routes match
.otherwise({
redirectTo: '/one'
});
});
Wenn Sie dann keine Änderungen an unseren Vorlagen vornehmen und nur einen neuen Link mit einer benutzerdefinierten Nachricht hinzufügen, wird die neue benutzerdefinierte Nachricht in unserer Ansicht angezeigt.
index.html
<div ng-app="myApp">
<nav>
<!-- links to switch routes -->
<a href="#/one">View One</a>
<a href="#/two">View Two</a>
<a href="#/three">View Three</a>
<!-- New link with custom message -->
<a href="#/three/This-is-a-message">View Three with "This-is-a-message" custom message</a>
</nav>
<!-- views will be injected here -->
<div ng-view></div>
<!-- templates can live in normal html files -->
<script type="text/ng-template" id="view-one.html">
<h1>{{ctrlOne.message}}</h1>
</script>
<script type="text/ng-template" id="view-two.html">
<h1>{{ctrlTwo.message}}</h1>
</script>
<script type="text/ng-template" id="view-three.html">
<h1>{{ctrlThree.message}}</h1>
</script>
</div>
Definieren von benutzerdefiniertem Verhalten für einzelne Routen
Die einfachste Art, ein benutzerdefiniertes Verhalten für einzelne Routen zu definieren, ist ziemlich einfach.
In diesem Beispiel verwenden wir es, um einen Benutzer zu authentifizieren:
1) routes.js : Erstellen requireAuth für jede gewünschte Route eine neue Eigenschaft (wie " requireAuth )
angular.module('yourApp').config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'templates/home.html',
requireAuth: true
})
.when('/login', {
templateUrl: 'templates/login.html',
})
.otherwise({
redirectTo: '/home'
});
}])
2) Überprüfen Sie in einem Top-Tier-Controller, der nicht an ein Element in der ng-view gebunden ist (um Konflikte mit dem $routeProvider zu vermeiden), ob die requireAuth Eigenschaft die Eigenschaft " newUrl hat, und requireAuth sich entsprechend
angular.module('YourApp').controller('YourController', ['$scope', 'session', '$location',
function($scope, session, $location) {
$scope.$on('$routeChangeStart', function(angularEvent, newUrl) {
if (newUrl.requireAuth && !session.user) {
// User isn’t authenticated
$location.path("/login");
}
});
}
]);