A brief analysis of the ui-router and ng-grid modules in angularJS
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>
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('app',['ui.router','loginModel','listModel']); app.config(function ($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/index'); $stateProvider .state('index',{ url: '/index', templateUrl: 'tpls/start.html' }) .state('register',{ url: '/register', templateUrl: 'tpls/register.html' }) .state('main',{ url: '/main{positionType:[0,9]{1,5}}', views: { '': { templateUrl: 'tpls/main.html' }, 'typeList@main': { templateUrl: 'tpls/typeList.html' }, 'tbHero@main': { templateUrl: 'tpls/tbHero.html' } } }) .state('addHero',{ url: '/addHero', templateUrl: 'tpls/addHero.html' }) .state('find',{ url: '/findPwd', templateUrl: 'tpls/findPwd.html' }) .state('detail',{ url: '/detail/:id', templateUrl: 'tpls/detail.html' }) })
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>
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>
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>
JS part
var listModel = angular.module('listModel',['ngGrid']); listModel.controller('listCtrl',['$scope','$http','$state','$stateParams', function ($scope, $http, $state, $stateParams) { $scope.pagingOptions = { pageSizes: [5,15,20], pageSize: 5, currentPage: 1 }; $scope.filterOptions = { filterText: '', useExternalFilter: true }; $scope.totalServerItems = 0; $scope.getDates = function (pageSize,page,/*optional*/searchText) { setTimeout(function () { if(searchText){ searchText = searchText.toLowerCase(); $http.get('data/hero.php?param='+$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('请求错误...'); }) }else{ $http.get('data/hero.php?param='+$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('请求错误...'); }) } },100); }; $scope.getDates($scope.pagingOptions.pageSize,$scope.pagingOptions.currentPage); $scope.$watch('pagingOptions', function () { $scope.getDates($scope.pagingOptions.pageSize,$scope.pagingOptions.currentPage); },true); $scope.$watch('filterOptions', function () { $scope.getDates($scope.pagingOptions.pageSize,$scope.pagingOptions.currentPage,$scope.filterOptions.filterText); },true); $scope.gridOptions = { data: 'datas', //表格中显示的数据来源 multiSelect: false, //是否能多选 enableRowSelection: false, //是否能选择行 enableCellSelection: true, //是否能选择单元格 enableCellEdit: false, //是否能修改内容 enablePinning: true, //是否被锁住了 columnDefs: [ { field: 'index', //这里是数据中的属性名 width: 80, display: '序号', //这里是表格的每一列的名称 pinnable: true, sortable: true //是否能排序 }, { field: 'name', displayName: '姓名', width: 120, sortable: true, pinnable: true }, { field:'alias', displayName:'别名', width: 60, sortable: true, pinnable: true }, { field:'position', displayName: '定位', width: 70, sortable: true, pinnable: true }, { field:'equip', displayName: '装备', width: 500, sortable: true, pinnable: true }, { field:'id', displayName: '详细攻略', sortable: false, pinnable: true, cellTemplate:'<div class="cellDetail"><a ui-sref="detail({id:row.getProperty(col.field)})" id="{{row.getProperty(col.field)}}">详情</a></div>' } ], enablePaging: true, //是否能翻页 showFooter: true, //是否显示表尾 totalServerItems: 'totalServerItems', //数据的总条数 pagingOptions: $scope.pagingOptions, //分页部分 filterOptions: $scope.filterOptions //数据过滤部分 } }])
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:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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.

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.

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'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 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 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.

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.
