Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the use of angular's routing ui-router

php中世界最好的语言
Release: 2018-03-16 17:12:29
Original
3376 people have browsed it

This time I will bring you a detailed explanation of the use of angular routing ui-router. What are the precautions when using angular routing ui-router. The following is a practical case, let's take a look.

UI-router

Installation: npm install --save angular-ui-router

Configure routing status

angular.module("myApp").config(function($stateProvider,$urlRouterProvider){
           $stateProvider
           .state({
                name:'main',
                url:'./',
                template('<div>this is a main</div>')
                     })
             .state({
                      name:'home',
                      url:'/home',
                      template:'<p>this is  home</p>'
              })
            .state({
                     name:'about',
                     url:'/about',
                     template:'<h3>Welcome hello</h3>'
                 })            //设置默认跳转
           $urlRouterProvider.otherwise('/')
           }
      )
Copy after login

Multiple modules, multiple routes, multiple Controller Processing method

Introduction module

   <script src="./angularjs/angular.js"></script>
    <script src="./js/ctrl1.js"></script>
    <script src="./js/ctrl2.js"></script>
    <script src="node_modules/angular-ui-router/release/angular-ui-router.js"></script>
Copy after login

Module dependency

var app = angular.module('myApp', ['ui.router', 'myApp.ctrl1', 'myApp.ctrl2']);
Copy after login

Routing configuration

app.config(function($stateProvider, $urlRouterProvider) {
        $stateProvider.state({
                name: 'main',
                url: '/my',
                templateUrl: './test.html',
                controller: 'ctrl1'
            })             /*
            1.设置一个为空匹配 url:'/my'
            2. 在增加一个 路由名字前半部份相同但是后面不同的名字
            * */
            .state({
                name:'my.page',
                url:'/:page'
            })
            .state({
                name: 'home',
                url: '/home',
                templateUrl: './angularjs/app.html',
                controller: 'ctrl2'
            })
            .state({
                name: 'about',
                url: '/about',
                template: '<div>about</div>',
                controller: 'ctrl3'
            })
        $urlRouterProvider.otherwise('/')
    })
Copy after login

$stateParams Get parameters.

Inject in the controller. Can get the parameters following the address bar.

-<ui-view ui-sref=&#39; &#39;></ui-view>
Copy after login

ui-sref can be used to pass parameters

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of JavaScript timer

Events and callback functions of JavaScript running mechanism

Detailed explanation of the multi-threading mechanism of the browser

The use of single-threaded JS and multi-threaded browsers

The above is the detailed content of Detailed explanation of the use of angular's routing ui-router. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template