首页
在线工具
搜索
1
Kuboard与KubeSphere的区别:Kubernetes管理平台对比
2
ShardingSphere使用中的重点问题剖析
3
Flowable工作流引擎源码深度解析
4
用AI生成的原型设计稿效果还可以
5
如何将Virtualbox和VMware虚拟机相互转换
杂谈与随笔
工具与效率
源码阅读
技术管理
运维
数据库
前端开发
后端开发
Search
标签搜索
Angular
Docker
Phabricator
SpringBoot
Java
Chrome
SpringSecurity
SpringCloud
DDD
Git
Mac
K8S
Kubernetes
ESLint
SSH
高并发
Eclipse
Javascript
Vim
Centos
Jonathan
累计撰写
86
篇文章
累计收到
0
条评论
首页
栏目
杂谈与随笔
工具与效率
源码阅读
技术管理
运维
数据库
前端开发
后端开发
页面
搜索到
17
篇与
的结果
2016-07-05
AngularJS 中的 $apply 和 $digest
AngularJS 中的 $apply 和 $digest 避免在控制器中使用 $watch 永远不要在控制器中使用 $watch,因为它会使控制器难以测试。Angular 提供了 $watchCollection 方法来对对象属性或数组元素设置浅监控,只要属性发生变化就会触发监听器回调。 $watchCollection 示例 $watchCollection() 函数也返回一个注销函数。调用这个注销函数时,也会取消集合上的监听。 手动调用 $apply() 如果你使用了 JavaScript 中的 setTimeout() 来更新一个 scope model,那么 AngularJS 就没有办法知道你更改了什么。这种情况下,调用 $apply() 是你的责任,通过调用它来触发一轮 $digest 循环。 示例代码 HTML <body ng-app="myApp"> <div ng-controller="MessageController"> Delayed Message: {{message}} </div> </body> JavaScript (不使用 $apply()) angular.module('myApp', []).controller('MessageController', function($scope) { $scope.getMessage = function() { setTimeout(function() { $scope.message = 'Fetched after 3 seconds'; console.log('message:' + $scope.message); }, 2000); } $scope.getMessage(); }); 这段代码会在控制台显示更新后的 model,但视图不会更新。 使用 $apply() 修改后的代码 angular.module('myApp', []).controller('MessageController', function($scope) { $scope.getMessage = function() { setTimeout(function() { $scope.$apply(function() { // wrapped this within $apply $scope.message = 'Fetched after 3 seconds'; console.log('message:' + $scope.message); }); }, 2000); } $scope.getMessage(); }); 现在视图会在两秒钟后更新。 注意: 应该使用 $timeout service 而不是 setTimeout(),因为前者会自动调用 $apply()。 $digest 循环 当 $digest 循环运行时,watchers 会被执行来检查 scope 中的 models 是否发生了变化。如果发生了变化,相应的 listener 函数就会被执行。为了处理 listener 函数可能修改 scope model 的情况,AngularJS 实现了脏检查机制(Dirty Checking)。这意味着 $digest 循环会持续运行直到 models 不再发生变化,或者循环次数达到10次。 结语 总是将要执行的代码和函数传递给 $apply 去执行。 使用 $apply 时,应该像下面这样: $scope.$apply(function() { $scope.variable1 = 'some value'; executeSomeAction(); }); 而不是这样: $scope.variable1 = 'some value'; executeSomeAction(); $scope.$apply(); 这两种方式虽然都能工作,但是第一个方法更安全,因为它能确保异常被正确捕获并处理。
2016年07月05日
2016-06-21
Angular-UI-Router 学习笔记
Angular-UI-Router 学习笔记 路由 (Route) 为什么用 Route AJAX 请求不会留下 History 记录 用户无法直接通过 URL 进入应用中的指定页面(无法保存书签、链接分享给朋友) AJAX 对 SEO 是个灾难 AngularJS 路由配置示例 var bookStoreApp = angular.module('bookStoreApp', [ 'ngRoute', 'ngAnimate', 'bookStoreCtrls', 'bookStoreFilters', 'bookStoreServices', 'bookStoreDirectives' ]); bookStoreApp.config( function($routeProvider) { $routeProvider .when('/hello', { templateUrl: 'tpls/hello.html', controller: 'HelloCtrl' }) .when('/list/:bookId', { templateUrl: 'tpls/bookList.html', controller: 'BookListCtrl' }) .otherwise({ redirectTo: '/hello' }); } ); 嵌套 View (Nested Routing for AngularJS) AngularUI-Router (AngularUI) <div ui-view></div> 前端路由的基本原理 哈希 (#) HTML5 中新的 history API 路由的核心 是给应用定义 "状态" 使用路由机制会影响应用的整体编码方式 (需预先定义好状态) 考虑兼容性问题与 "优雅降级" Angular-UI-Router ui-sref 指令 <a ui-sref="home">Home</a> <a ui-sref="about">About</a> <a ui-sref="contacts.list">Contacts</a> $state.includes 返回 true / false <li ng-class="{active: $state.includes('contacts')}"> <a ui-serif="contacts.list">Contacts</a> </li> ui-sref-active <li ui-sref-active="active"> <a ui-sref="about">About</a> </li> 包含模块 angular.module('uiRouter', ['ui.router']); 方便获得当前状态的方法 (绑到根作用域) app.run(['$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams) { $rootScope.$state = $state; $rootScope.$stateParams = $stateParams; } ]); 路由重定向 ($urlRouterProvider) app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) { $urlRouterProvider .when('/c?id', '/contacts/:id') .when('/user/:id', '/contacts/:id') .otherwise('/'); } ]); 状态配置 $stateProvider. state('about', { url: '/about', template: '<h1>Welcome to UI-Router Demo</h1>', // 使用 templateProvider 处理异步加载 templateProvider: ['$timeout', function($timeout) { return $timeout(function() { return '<p class="lead">UI-Router Resource</p>' + '<p>The second line</p>'; }, 100); } ], templateUrl: 'about.html', controller: 'UIRouterCtrl', controller: ['$scope', '$state', 'contacts', 'utils', function ($scope, $state, contacts, utils) { $scope.contacts = contacts; $scope.goToRandom = function () { var randId = utils.newRandomKey($scope.contacts, 'id', $state.params.contactId); $state.go('contacts.details', { contactId: randId }); } } ] }); 嵌套路由 (Nested Router) 父级路由 (Parent Router) $stateProvider. state('contacts', { abstract: true, url: '/contacts', templateUrl: 'contacts.html', // 处理异步数据调用 resolve: { contacts: ['contacts', function(contacts) { return contacts.all(); } ] }, controller: ['$scope', '$state', 'contacts', 'utils', function ($scope, $state, contacts, utils) { $scope.contacts = contacts; $scope.goToRandom = function () { var randId = utils.newRandomKey($scope.contacts, 'id', $state.params.contactId); $state.go('contacts.details', { contactId: randId }); } } ] }); 子级路由 (Children Router) $stateProvider .state('contacts.list', { url: '', templateUrl: 'contacts.list.html' }) .state('contacts.detail', { url: '/{contactId:[0-9]{1,4}}', views: { // 默认 view '': { templateUrl: 'contacts.detail.html', controller: ['$scope', '$stateParams', 'utils', function ($scope, $stateParams, utils) { $scope.contact = utils.findById($scope.contacts, $stateParams.contactId); } ] }, // 其他 view 'hint@': { template: 'This is contacts.detail populating the "hint" ui-view' }, 'menuTip': { templateProvider: ['$stateParams', function($stateParams) { return '<hr><small class="muted">Contact ID: ' + $stateParams.contactId + '</small>'; } ] } } }); HTML 代码 <h2>All Contacts</h2> <ul> <li ng-repeat="contact in contacts"> <a ui-sref="contacts.detail({contactId:contact.id})">{{contact.name}}</a> </li> </ul>
2016年06月21日
1
...
3
4