特别好用的指令
来自 http://techlogs.cn/angular-bi-jiao-chang-yong-de-zhi-ling/
angular.module('my.directives').directive('autoselect', [function () {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (element.is("input") && attr.type === "text") {
var selected = false;
var time = parseInt(attr["autoselect"]);
element.bind("mouseup", function (e) {
if (selected) {
e.preventDefault();
e.stopPropagation();
}
selected = false;
});
if (time > 0) {
element.bind("focus", function (event) {
setTimeout(function () {
selected = true;
event.target.select();
}, time);
});
} else {
element.bind("focus", function (event) {
selected = true;
event.target.select();
});
}
}
}
};
}]);
angular.module('my.directives').directive('clickOutside', ['$document', function ($document) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
$(element).bind('mousedown', function (e) {
e.preventDefault();
e.stopPropagation();
});
$("#" + attrs["insideId"]).bind('mousedown', function (e) {
e.stopPropagation();
});
$("#" + attrs["insideId"]).bind('blur', function (e) {
setTimeout(function () {
scope.$apply(attrs.clickOutside);
});
});
$document.bind('mousedown', function () {
scope.$apply(attrs.clickOutside);
});
}
};
}]);
angular.module('my.directives').directive('clickInside', ['$document', function ($document) {
return {
restrict: 'A',
link: function (scope, element, attrs, ctrl) {
$(element).bind('focus click', function (e) {
scope.$apply(attrs.clickInside);
e.stopPropagation();
});
}
};
}]);
angular.module('my.directives').directive('scrollInside', function () {
return {
restrict: 'A',
link: function (scope, element, attrs, ctrl) {
$(element).bind('scroll', function (e) {
scope.$apply(attrs.scrollInside);
e.stopPropagation();
});
}
};
});
angular.module('my.directives').directive('bindKeyBoardEvent', function () {
return {
restrict: 'A',
link: function (scope, element, attrs, ctrl) {
$(element).bind('focus click', function (e) {
scope.$apply(attrs.bindKeyBoardEvent);
e.stopPropagation();
});
}
};
});
angular.module('my.directives').directive('myDraggable', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (attr["modal"] !== undefined) {
scope.$watch(attr["modal"], function (newValue) {
if (newValue) {
setTimeout(function () {
$(".modal").draggable({handle: ".modal-header"});
}, 100);
} else {
$(".modal").attr("style", "");
}
}, true);
$(window).resize(function () {
$(".modal").attr("style", "");
});
} else {
element.draggable($parse(attr["hrDraggable"])(scope));
}
}
};
}]);
angular.module('my.directives').directive('myResizable', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (attr["modal"] !== undefined) {
scope.$watch(attr["modal"], function (newValue) {
if (newValue) {
setTimeout(function () {
$(".modal").resizable({handles: "e, w"});
}, 100);
}
}, true);
} else {
element.resizable($parse(attr["hrResizable"])(scope));
}
}
};
}]);
angular.module('my.directives').directive("conditionFocus", [function () {
return function (scope, element, attrs) {
var dereg = scope.$watch(attrs.conditionFocus, function (newValue) {
if (newValue) {
element.focus();
}
});
element.bind("$destroy", function () {
if (dereg) {
dereg();
}
});
};
}]);
angular.module('my.directives').directive("scrollToHide", [function () {
return function (scope, element, attrs) {
var height= parseFloat(attrs.maxHeight)
$(window).scroll(function(){
var scrollTop= document.body.scrollTop||document.documentElement.scrollTop;
if(scrollTop>height){
$parse(attrs.ngShow).assign(scope, false);
}else{
$parse(attrs.ngShow).assign(scope, true);
}
})
};
}]);
angular.module('my.directives').directive("resetToZero", ["$parse", function ($parse) {
return function (scope, element, attrs) {
var dereg = scope.$watch(attrs.resetToZero, function (newValue) {
if (newValue) {
if (attrs.ngModel) {
$parse(attrs.ngModel).assign(scope, 0);
}
}
});
element.bind("$destroy", function () {
if (dereg) {
dereg();
}
});
};
}]);
angular.module('my.directives').directive("resetToEmptyString", ["$parse", function ($parse) {
return function (scope, element, attrs) {
var dereg = scope.$watch(attrs.resetToEmptyString, function (newValue) {
if (newValue) {
if (attrs.ngModel) {
var _getter = $parse(attrs.ngModel);
if (_getter(scope)) {
_getter.assign(scope, "");
} else {
_getter.assign(scope.$parent, "");
}
}
}
});
element.bind("$destroy", function () {
if (dereg) {
dereg();
}
});
};
}]);
angular.module('my.directives').directive("numberOnly", ["$parse", function ($parse) {
return function (scope, element, attrs) {
element.bind("keyup", function () {
if(event.keyCode==37||event.keyCode== 39){
return false;
}
var val = element.val().replace(/[^\d.]/g, '');
if(attrs.max){
if(val>parseInt(attrs.max)){
val=attrs.max;
}
}
element.val(val);
if (attrs.ngModel) {
$parse(attrs.ngModel).assign(scope, val);
}
return false;
});
element.bind("afterpaste", function () {
var val = element.val().replace(/[^\d.]/g, '');
if(attrs.max){
if(val>parseInt(attrs.max)){
val=attrs.max;
}
}
element.val(val);
if (attrs.ngModel) {
$parse(attrs.ngModel).assign(scope, val);
}
return false;
});
};
}]);
angular.module('my.directives').directive("upperCaseOnly", ["$parse", function ($parse) {
return function (scope, element, attrs) {
element.bind("keyup", function () {
var val = element.val().toUpperCase();
element.val(val);
if (attrs.ngModel) {
$parse(attrs.ngModel).assign(scope, val);
}
return false;
});
element.bind("afterpaste", function () {
var val =element.val().toUpperCase();
element.val(val);
if (attrs.ngModel) {
$parse(attrs.ngModel).assign(scope, val);
}
return false;
});
};
}]);
angular.module('my.directives').directive("noSpecialString", ["$parse", function ($parse) {
return function (scope, element, attrs) {
element.bind("keyup", function () {
var val = element.val().replace(/[\W]/g, '');
element.val(val);
if (attrs.ngModel) {
$parse(attrs.ngModel).assign(scope, val);
}
return false;
});
element.bind("afterpaste", function () {
var val = element.val().replace(/[^\d]/g, '');
element.val(val);
if (attrs.ngModel) {
$parse(attrs.ngModel).assign(scope, val);
}
return false;
});
};
}]);
angular.module('my.directives').directive("round2bit", ['$parse', '$filter', function ($parse, $filter) {
return function ($scope, element, attrs) {
element.blur(function () {
if (attrs.ngModel) {
var _getter = $parse(attrs.ngModel);
var _numberStr2Round = (_getter($scope) || 0);
_getter.assign($scope, $filter('number')(_numberStr2Round, 2).split(",").join(""));
$scope.$apply();
}
});
};
}]);
angular.module('hr.directives').directive('SelfHeight', ['$timeout', function ($timeout) {
function _resizeElement(element, SelfHeight) {
element.height((typeof SelfHeight === "number") ? SelfHeight : eval(SelfHeight));
};
return {
priority: 1000,
link: function (scope, element, attrs) {
var hrSelfHeight = attrs["SelfHeight"];
var on = attrs["on"];
if (on) {
$(window).resize(function () {
_resizeElement(element, scope.$eval(SelfHeight));
});
scope.$watch(on, function () {
$timeout(function () {
_resizeElement(element, scope.$eval(SelfHeight));
}, 100);
}, true);
} else {
$(window).resize(function () {
_resizeElement(element, SelfHeight);
});
_resizeElement(element, SelfHeight);
}
}
};
}]);