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

Sample code for routing Ui-router module usage in AngularJS

黄舟
Release: 2017-05-31 10:22:45
Original
1310 people have browsed it

This article mainly introduces the AngularJSroutingUi-router module usage, and analyzes the functions, usage and related precautions of the Ui-router module in the form of examples. , friends in need can refer to

This article describes the usage of AngularJS routing Ui-router module with examples. Share it with everyone for your reference, as follows:

Due to some design reasons, AngularJS’s native routing module has some shortcomings, such as not supporting nesting of views, etc., so many communities have begun to design their own routing modules. , the most representative one is ui-route.

ui-route is a powerful routing module that enhances other functions on the native ng-route module.

Now let’s make a few DEMOs to get in touch with ui-route.

<!--初始页面-->
<!doctype html>
<meta charset="UTF-8">
<html>
<head>
  <link href="self.css" rel="external nofollow" rel="stylesheet">
</head>
<body >
<p ng-app="myApp">
<p><a ui-sref = "index">首页</a></p>
<p ui-view></p><!--这里是路由视图存放的地方-->
</p>
<script src="angular.min.js"></script>
<script src="angular-ui-router.js"></script>
<script src="test2.js"></script>
</body>
</html>
Copy after login

First you must reference the angular-ui-router.js file. This file is different from the angular-route.js file of AngularJs. And the file must be placed under angular.min.js.

Looking at the body code of Html, you can find that there are three places that are different from the body code when using native ng-route. They are ui-sref, index and ui-view respectively. Skip it first and take a look at how to initialize the ui-route module.

Initialize the ui-route module:

var app = angular.module(&#39;myApp&#39;,[&#39;ui.router&#39;]);
app.config(["$stateProvider",function($stateProvider){
  $stateProvider
    .state("index",{
      url:&#39;/&#39;,
      template:&#39;<p>我是首页内容</p>&#39;
    })
}]);
Copy after login

First of all, similar to the native ng-route routing module, ui-route must be injected first. Then proceed to specific configuration. Different from the native ng-route, ui-route uses state() instead of the native when(). It adds a new parameter based on when(), here is index, Used to distinguish which command this part of the route responds to.

Go back to the previous

Homepage

, and you will probably know the relationship between their views and it . ui-view replaces the previous ng-view, ui-sref replaces the previous ng-href, and it no longer points to the link, but to the name of "Navigation".

The url attribute can uniquely identify the subsequent address of the route to distinguish it from subsequent routes.

The above is the detailed content of Sample code for routing Ui-router module usage in AngularJS. 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