angular 全局常用指令

1.全局支持 enter快捷键触发事件

		// 全局指令
		app.directive('ngEnter', ['$window',"$timeout", ($window,$timeout) => {
			return {
				restrict: 'A',
				link ($scope, element, attrs) {
					element.bind('keypress', (ev) => {
						ev = ev || $window.event;
						if (event.keyCode === 13) {
                            $timeout(function () {
                                $scope.$eval(attrs.ngEnter);
                            });
                            //引入$timeout 解决渲染问题
                            ev.preventDefault ? ev.preventDefault() : ev.returnValue = false;
							return false;
                        }
                        $window.event ? ev.cancelBubble=true : ev.stopPropagation();//清除冒泡
					});
					element.on('$destroy', function () {
						element.unbind('keypress');
						$scope.$destroy();
					});
				}
			};
		}]);

2.点击按钮之后禁用按钮,防止多重提交

	app.directive('clickAndDisable', function() {
			return {
			  scope: {
				clickAndDisable: '&'
			  },
			  link: function(scope,element, iAttrs) {
				element.bind('click', function() {
				 element.prop('disabled',true);
				  scope.clickAndDisable().finally(function() {
					element.prop('disabled',false);
				  })
				});
			  }
			};
		  });

3.判断 ng-repeat 渲染完成后操作

	app.directive('repeatFinish',function(){
			return {
			  link: function(scope,element,attr){
				if(scope.$last == true){
                                   console.log("ng-repeat遍历完成");
				  scope.$eval( attr.repeatFinish )
				}
			  }
			}
		  });
原文地址:https://www.cnblogs.com/yaohe/p/11046876.html