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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Mar 15, 2025 am 09:19 AM

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles