AngularJS route: ng-route and ui-router
The ng-route module of AngularJS provides [Deep-Linking] URLs for controllers and views. In general, in the ng-route Module$route
Service monitoring$location.url()
And map it to a pre-defined controller. That is, URL routing on the client. First$route
And then introduce a more powerful client routing framework ui-router.
Angular Routing
Define controllers for multiple pages in the APP and provide the corresponding template. Then$routeProvider
To map URLs to these controllers and views. First define a basic Angular APP and introducengRoute
:
Angular$route
Service inngRoute
Module. You need to introduce the corresponding javascript file, and in our APPngRoute
Add as a module dependency (how to add a module dependency ?).
var app = angular.module('ngRouteExample', ['ngRoute']) .controller('MainController', function($scope) { }) .config(function($routeProvider, $locationProvider) { $routeProvider .when('/http://blog.csdn.net/yangjvn/article/details/users', { templateUrl: 'user-list.html', controller: 'UserListCtrl' }) .when('/http://blog.csdn.net/yangjvn/article/details/users/:username', { templateUrl: 'user.html', controller: 'UserCtrl' }); // configure html5 $locationProvider.html5Mode(true); });
In the above Code,$routeProvider
Defines the ing between two URLs:/http://blog.csdn.net/yangjvn/article/details/users
Useuser-list.html
As a template,UserListCtrl
As a controller;/http://blog.csdn.net/yangjvn/article/details/users/:username
Will match similar/http://blog.csdn.net/yangjvn/article/details/users/alice
URL, you will see how to get:username
The matched value. First look at the homepage template:
HTML5Mode: the URL of the server-side route and client route is#
Separated. For example/foo/bar#/http://blog.csdn.net/yangjvn/article/details/users/alice
Angular routes through the operation anchor. Howeverhtml5Mode(true)
Will be removed#
, URL/foo/bar/http://blog.csdn.net/yangjvn/article/details/users/alice
(This requires the browser to support HTML5, because Angular usespushState
). In this case, the server needs to return the home page (/foo/bar
) View, and then route it to Angular/foo/bar/http://blog.csdn.net/yangjvn/article/details/users/alice
View.
Choose: user list | user: alice
Note that there isdiv[ng-view]
, The Child page will be loaded here.
Route Parameters
Then we define the subpage controller and view template of the preceding route configuration. User List page:
app.controller('UserListCtrl', function($scope) {});
User List Page
User page:
app.controller('UserCtrl', function($scope, $routeParams) { $scope.params = $routeParams;});
User Page
Click/http://blog.csdn.net/yangjvn/article/details/users/alice
Will Loaduser.html
,span
Isalice
. $ RouteParams provides the current route parameters, for example:
// Given:// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby// Route: /Chapter/:chapterId/Section/:sectionId//// Then$routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}
In addition to $ routeParams, Angular also provides $ location to get and set URLs.
UI-Router
UI-Router is a client routing framework provided by Angular-UI. It solves many native ng-route shortcomings:
- View cannot be nested. This means
$scope
Unnecessary re-loading will occur. This is also introduced in Onboard.ui-route
.
- Multiple views are not supported under the same URL. This requirement is also common: we want the navigation bar to use one view (and the corresponding Controller), and the content part to use another view (and the corresponding Controller ).
UI-Router proposed$state
. One$state
Is the status of the current navigation and UI, each$state
You need to bind a URL Pattern. In the controller and template$state
For URL redirection and routing. This is a simple example:
// in app-states.js$stateProvider .state('contacts', { url: '/contacts', template: 'contacts.html', controller: 'ContactCtrl' }) .state('contacts.detail', { url: /contacts/:contactId, templateUrl: 'contacts.detail.html', controller: function ($stateParams) { // If we got here from a url of /contacts/42 $stateParams.contactId === 42; } });
When accessing/contacts
,contacts
$state
Activated to load the corresponding controller and view. Generally$state
Instead of directly performing URL operations. For example, use $ state. go in the script:
$ State. go ('contacts'); // specify the state name, which is equivalent to redirecting to/contacts $ state. go ('contacts. detail ', {contactId: 42}); // jump to/contacts/42
Use ui-sref in the template (this is a direve VE ):
ContactsContact 42
Nested ViewUnlike native ng-route of Angular, the ui-router view can be nested, And the view nesting usually corresponds to the nesting of $ state.contacts.detail
Yescontacts
Child$state
,contacts.detail.html
Will also actcontacts.html
Sub-page:
My Contacts
Aboveui-view
Usage andng-view
It looks very similar, but the difference isui-view
Can be used together$state
Nesting at any level, that iscontacts.detail.html
Can still containui-view
, Its$state
It may becontacts.detail.hobbies
.
Naming ViewIn ui-router$state
There can be multiple views under, they have their own templates and controllers. This is not what ng-route does. It gives the front-end routing great flexibility. Here is an example:
This template contains three namedui-view
You can set templates and controllers for them:
$stateProvider .state('report',{ views: { 'filters': { templateUrl: 'report-filters.html', controller: function($scope){ ... controller stuff just for filters view ... } }, 'tabledata': { templateUrl: 'report-table.html', controller: function($scope){ ... controller stuff just for tabledata view ... } }, 'graph': { templateUrl: 'report-graph.html', controller: function($scope){ ... controller stuff just for graph view ... } } } })