Home Web Front-end JS Tutorial A brief analysis of the ui-router and ng-grid modules in angularJS

A brief analysis of the ui-router and ng-grid modules in angularJS

Dec 28, 2016 pm 02:19 PM

I was bored at home and happened to find a tutorial about angular on the Internet. I studied the two modules of angular ui-router and ng-grid, and imitated it and made a small thing.

The code has been uploaded to github, the address is here https://github.com/wwervin72/Angular.

Interested friends can take a look. So here we will first understand the usage of these two modules.

Let’s first talk about the ui-router module. This module is mainly used to implement deep routing. In fact, angular has a built-in instruction ng-route. If there are no nesting problems in the project, it is quite convenient to use this instruction to jump between pages. However, its shortcoming is that it has deep knowledge. There is no way around hierarchical nested routing. So first, if we want to use this module, we need to download it.

The download address is here http://www.bootcdn.cn/angular-ui-router/.

After downloading it, we can import it into our project. We should pay attention here, because this module is based on angular, so before this, we also need to import angular's js file. This can be downloaded from angular's official website.

After the above preparations are completed, we can start writing our code.

HTML part

<div class="container">
 <div ui-view>
 
 </div>
</div>
Copy after login

One thing to note here is that the attribute added in the div is no longer ng-view, but ui-view.

JS part

var app=angular.module(&#39;app&#39;,[&#39;ui.router&#39;,&#39;loginModel&#39;,&#39;listModel&#39;]);
 
app.config(function ($stateProvider, $urlRouterProvider) {
 $urlRouterProvider.otherwise(&#39;/index&#39;);
 $stateProvider
 .state(&#39;index&#39;,{
  url: &#39;/index&#39;,
  templateUrl: &#39;tpls/start.html&#39;
 })
 .state(&#39;register&#39;,{
  url: &#39;/register&#39;,
  templateUrl: &#39;tpls/register.html&#39;
 })
 .state(&#39;main&#39;,{
  url: &#39;/main{positionType:[0,9]{1,5}}&#39;,
  views: {
  &#39;&#39;: {
   templateUrl: &#39;tpls/main.html&#39;
  },
  &#39;typeList@main&#39;: {
   templateUrl: &#39;tpls/typeList.html&#39;
  },
  &#39;tbHero@main&#39;: {
   templateUrl: &#39;tpls/tbHero.html&#39;
  }
  }
 })
 .state(&#39;addHero&#39;,{
  url: &#39;/addHero&#39;,
  templateUrl: &#39;tpls/addHero.html&#39;
 })
 .state(&#39;find&#39;,{
  url: &#39;/findPwd&#39;,
  templateUrl: &#39;tpls/findPwd.html&#39;
 })
 .state(&#39;detail&#39;,{
  url: &#39;/detail/:id&#39;,
  templateUrl: &#39;tpls/detail.html&#39;
 })
})
Copy after login

There are three places to pay attention to here:

1. When nesting, the outermost layer here is the main part, and then nested inside After covering the typeList and tbHero parts, we need to pay attention to the writing here.

2. When we need to open different content based on different selections, we need to pass parameters to the next page. Here is the detail part. We also need to pay more attention to the writing here.

3. When we use angular.module to create an app application, we need to import the ui.router module into it. In addition, some modules we created ourselves also need to be imported here.

4. We use $stateProvider to configure routing here instead of $routeProvider, and state is used here instead of when.

After the routing is configured here, the only thing left is to write the code for each part of tpls. I won’t go into too much introduction here. The most important thing here is the routing configuration.

Okay, let’s take a look at the usage of ng-grid. Here is the download address http://www.bootcdn.cn/ng-grid/.

HTML part

main part

<div class="row">
 <div class="col-sm-2" ui-view="typeList">
 
 </div>
 <div class="col-sm-10" ui-view="tbHero">
 
 </div>
</div>
Copy after login

typeList part

<div class="row">
 <div class="col-sm-12">
 <div class="list-group">
  <a href="javascript:void(0);" class="list-group-item active">英雄定位类型</a>
  <a ui-sref="main({positionType:0})" class="list-group-item">全部定位</a>
  <a ui-sref="main({positionType:1})" class="list-group-item">射手</a>
  <a ui-sref="main({positionType:2})" class="list-group-item">中单</a>
  <a ui-sref="main({positionType:3})" class="list-group-item">上单</a>
  <a ui-sref="main({positionType:4})" class="list-group-item">打野</a>
  <a ui-sref="main({positionType:5})" class="list-group-item">辅助</a>
 </div>
 </div>
</div>
Copy after login

tbHero part

<div ng-controller="listCtrl">
 <div class="row">
 <div class="col-sm-3">
  <button class="btn btn-success" ui-sref="addHero()">添加英雄</button>
  <button class="btn btn-warning" ui-sref="index()">退出</button>
 </div>
 <div class="col-sm-9">
  <form class="form-horizontal">
  <input type="text" ng-model="filterOptions.filterText" placeholder="请输入查询关键字..." class="form-control searchText"/>
  </form>
 </div>
 </div>
 <div class="row">
 <div class="col-sm-12">
  <div class="gridStyle" ng-grid="gridOptions">
 
  </div>
 </div>
 </div>
</div>
Copy after login

JS part

var listModel = angular.module(&#39;listModel&#39;,[&#39;ngGrid&#39;]);
listModel.controller(&#39;listCtrl&#39;,[&#39;$scope&#39;,&#39;$http&#39;,&#39;$state&#39;,&#39;$stateParams&#39;, function ($scope, $http, $state, $stateParams) {
 
 $scope.pagingOptions = {
 pageSizes: [5,15,20],
 pageSize: 5,
 currentPage: 1
 };
 
 $scope.filterOptions = {
 filterText: &#39;&#39;,
 useExternalFilter: true
 };
 
 $scope.totalServerItems = 0;
 $scope.getDates = function (pageSize,page,/*optional*/searchText) {
 setTimeout(function () {
  if(searchText){
  searchText = searchText.toLowerCase();
  $http.get(&#39;data/hero.php?param=&#39;+$stateParams.positionType).success(function (data) {
   var data = data.filter(function (item) {
   return JSON.stringify(item).indexOf(searchText) != -1;
   })
   data.forEach(function (item,i) {
   item.index = i+1;
   });
   $scope.totalServerItems = data.length;
   $scope.datas=data.slice((page-1)*pageSize,page*pageSize);
  }).error(function (data) {
   alert(&#39;请求错误...&#39;);
  })
  }else{
  $http.get(&#39;data/hero.php?param=&#39;+$stateParams.positionType).success(function (data) {
   data.forEach(function (item,i) {
   item.index = i+1;
   });
   $scope.totalServerItems = data.length;
   $scope.datas = data.slice((page-1)*pageSize,page*pageSize);
  }).error(function (data) {
   alert(&#39;请求错误...&#39;);
  })
  }
 },100);
 };
 $scope.getDates($scope.pagingOptions.pageSize,$scope.pagingOptions.currentPage);
 $scope.$watch(&#39;pagingOptions&#39;, function () {
 $scope.getDates($scope.pagingOptions.pageSize,$scope.pagingOptions.currentPage);
 },true);
 $scope.$watch(&#39;filterOptions&#39;, function () {
 $scope.getDates($scope.pagingOptions.pageSize,$scope.pagingOptions.currentPage,$scope.filterOptions.filterText);
 },true);
 
 $scope.gridOptions = {
 data: &#39;datas&#39;,  //表格中显示的数据来源
 multiSelect: false, //是否能多选
 enableRowSelection: false, //是否能选择行
 enableCellSelection: true, //是否能选择单元格
 enableCellEdit: false, //是否能修改内容
 enablePinning: true,  //是否被锁住了
 columnDefs: [
  {
  field: &#39;index&#39;, //这里是数据中的属性名
  width: 80,
  display: &#39;序号&#39;, //这里是表格的每一列的名称
  pinnable: true,
  sortable: true  //是否能排序
  }, 
  {
  field: &#39;name&#39;,
  displayName: &#39;姓名&#39;,
  width: 120,
  sortable: true,
  pinnable: true
  },
  {
  field:&#39;alias&#39;,
  displayName:&#39;别名&#39;,
  width: 60,
  sortable: true,
  pinnable: true
  },
  {
  field:&#39;position&#39;,
  displayName: &#39;定位&#39;,
  width: 70,
  sortable: true,
  pinnable: true
  },
  {
  field:&#39;equip&#39;,
  displayName: &#39;装备&#39;,
  width: 500,
  sortable: true,
  pinnable: true
  },
  {
  field:&#39;id&#39;,
  displayName: &#39;详细攻略&#39;,
  sortable: false,
  pinnable: true,
  cellTemplate:&#39;<div class="cellDetail"><a ui-sref="detail({id:row.getProperty(col.field)})" id="{{row.getProperty(col.field)}}">详情</a></div>&#39;
  }
 ],
 enablePaging: true, //是否能翻页
 showFooter: true,  //是否显示表尾
 totalServerItems: &#39;totalServerItems&#39;, //数据的总条数 
 pagingOptions: $scope.pagingOptions, //分页部分
 filterOptions: $scope.filterOptions  //数据过滤部分
 }
}])
Copy after login

The most important thing here is $scope.gridOptions. At the same time, we need to pay more attention to the writing method of passing parameters in the last detailed guide.

A few renderings are attached below:

A brief analysis of the ui-router and ng-grid modules in angularJS

A brief analysis of the ui-router and ng-grid modules in angularJS

A brief analysis of the ui-router and ng-grid modules in angularJS

## Also here The content about angular form verification, service, wizard, php, etc. also used in it will not be introduced here. If there is anything wrong, please leave a message to let me know, thank you ^_^.

The above brief analysis of the ui-router and ng-grid modules in angularJS is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more articles on the ui-router and ng-grid modules in angularJS, please pay attention to the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
JavaScript Engines: Comparing Implementations JavaScript Engines: Comparing Implementations Apr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

From C/C   to JavaScript: How It All Works From C/C to JavaScript: How It All Works Apr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript and the Web: Core Functionality and Use Cases JavaScript and the Web: Core Functionality and Use Cases Apr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript in Action: Real-World Examples and Projects JavaScript in Action: Real-World Examples and Projects Apr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding the JavaScript Engine: Implementation Details Understanding the JavaScript Engine: Implementation Details Apr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Python vs. JavaScript: Development Environments and Tools Python vs. JavaScript: Development Environments and Tools Apr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

See all articles