Open msforest opened 7 years ago
ng-stats
插件可以帮助跟踪watches的数量和digest循环。ng-if
和ng-show/ng-hide
的不同?ng-show/ng-hide
总是会插入到DOM中,通过条件判断来决定display的属性值显示与否;ng-if
根据条件判断是否插入到DOM中;当我们判断是否显示DOM的时候,用ng-if
总是比ng-show/ng-hide
效果好。digest
的循环,AngularJS会对scope数据值进行新老值的比较;digest
循环是自动触发的,也可以通过$apply
手动触发digest
循环。
For more information, take a look in the ng-book explanation: The Digest Loop and $applydirective
和component
适配成1.5的components
$scope.$apply()
是怎么工作的?$scope.$apply()
是AngularJS的一个核心方法,强制angular引擎运行所有watch上的变量。ng-if
angular.factory('testService', function($q){
return {
getName: function(){
var deferred = $q.defer();
//API call here that returns data
testAPI.getName().then(function(name){
deferred.resolve(name)
});
return deferred.promise;
}
}
});
restrict
来指定指令的类型:A/E/C/M$timeout/$interval
,如何取消$watch
?var customTimeout = $timeout(function () {
// arbitrary code
}, 55);
$timeout.cancel(customTimeout);
///////
var deregisterWatchFn = $scope.$on(‘$destroy’, function () {
// we invoke that deregistration function, to disable the watch
deregisterWatchFn();
});
$scope
是什么?var directiveDefinitionObject = {
priority: 0,
template: '<div></div>', // or // function(tElement, tAttrs) { ... },
// or
// templateUrl: 'directive.html', // or // function(tElement, tAttrs) { ... },
transclude: false,
restrict: 'A',
templateNamespace: 'html',
scope: false,
controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... },
controllerAs: 'stringIdentifier',
bindToController: false,
require: 'siblingDirectiveName', // or // ['^parentDirectiveName', '?optionalDirectiveName', '?^optionalParent'],
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
// or
// return function postLink( ... ) { ... }
},
// or
// link: {
// pre: function preLink(scope, iElement, iAttrs, controller) { ... },
// post: function postLink(scope, iElement, iAttrs, controller) { ... }
// }
// or
// link: function postLink( ... ) { ... }
};"
compile
方法。因为在执行和转换之前,compile
方法有权修改指令模板,所以compile
方法是安全地处理模板的。ng-pattern
内置指令用正则表达式匹配。AngularJS提供了一个内置的错误处理服务叫$exceptionHandler
。
myApp.factory('$exceptionHandler', function($log, ErrorService) {
return function(exception, cause) {
if (console) {
$log.error(exception);
$log.error(cause);
}
ErrorService.send(exception, cause);
};
});
使用$watch
方法实现
function MyCtrl($scope) {
$scope.email = "";
$scope.$watch("email", function(newValue, oldValue) {
if ($scope.email.length > 0) {
console.log("User has started writing into email");
}
});
}
ng-disable
指令绑定到checkbox的值
<body ng-app>
<label><input type="checkbox" ng-model="checked"/>Disable Button</label>
<button ng-disabled="checked">Select me</button>
</body>
指令(Directives)是所有AngularJS应用最重要的部分。尽管AngularJS已经提供了非常丰富的指令,但还是经常需要创建应用特定的指令。
自定义指令
指令可以有四种表现形式,分别为元素(element)、属性(attribute)、注释(comment)、样式类(class),正好代表ECMAScript的前四个字母,还是很好记的。基本样式如下:
使用app.directive()方法在模块中注册一个新的指令,第一个参数是指令的名称,第二个参数是返回指令定义对象的函数;如果你的指令依赖于其他的对象或者服务,比如 $rootScope, $http, 或者$compile,他们可以在这个时间被注入。 页面使用形式:
部分属性说明:
angularjs深度学习参考