Angular 常用指令實例總結(jié)整理
Angular 常用指令
已經(jīng)用了angular很久積累了一些很實用的指令,需要的話直接拿走用,有問題大家一起交流
1.focus時,input:text內(nèi)容全選
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();
});
}
}
}
};
}]);
2.clickOutside指令,外部點擊時觸發(fā),click-outside="func()" func為自己指定的方法,一般為關(guān)閉當(dāng)前層的方法,inside-id="" 點擊指定id的輸入框時,當(dāng)前層不關(guān)閉
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);
});
}
};
}]);
3.clickInside指令,內(nèi)部點擊時觸發(fā)
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();
});
}
};
}]);
4.scrollInside 指令 ,內(nèi)部滾動時觸發(fā)
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();
});
}
};
});
5. bindKeyBoardEvent指令,內(nèi)部獲得焦點或者點擊時觸發(fā)
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();
});
}
};
});
6. myDraggable 使元素可拖動
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));
}
}
};
}]);
6.myResizable 使元素可拖拽改變尺寸大小
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));
}
}
};
}]);
6. conditionFocus 作為一個元素的屬性存在:如果監(jiān)聽的表達(dá)式值為true,則將焦點放到本元素上。
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();
}
});
};
}]);
7.scrollToHide 滾動到頂部的時候觸發(fā)
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);
}
})
};
}]);
8.resetToZero 作為一個元素的屬性存在:如果監(jiān)聽的表達(dá)式值為true,則將本元素中所綁定的ngModel值設(shè)為0
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();
}
});
};
}]);
9.resetToEmptyString 作為一個元素的屬性存在:如果監(jiān)聽的表達(dá)式值為true,則將本元素中所綁定的ngModel值設(shè)為空字符串。
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();
}
});
};
}]);
10. numberOnly 輸入框內(nèi)容僅限數(shù)值的指令(輸入內(nèi)容不允許為 負(fù)值),可以設(shè)定最大值(max屬性)
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;
});
};
}]);
11. upperCaseOnly 輸入框自動轉(zhuǎn)換成大寫
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;
});
};
}]);
12. noSpecialString 輸入框內(nèi)容不能為特殊字符
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;
});
};
}]);
13. round2bit 輸入框失去焦點 保留兩位小數(shù)
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();
}
});
};
}]);
14.SelfHeight dom編譯期設(shè)置元素高度,可以接受數(shù)字或者表達(dá)式
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);
}
}
};
}]);
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
利用Angularjs中模塊ui-route管理狀態(tài)的方法
這篇文章主要給大家介紹了如何用Angularjs中的模板ui-route來管理狀態(tài)的方法,文中通過示例代碼介紹的很詳細(xì),相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價值,有需要的朋友可以一起來學(xué)習(xí)學(xué)習(xí)。2016-12-12
Angular懶加載模塊與Combined?Injector原理全面解析
這篇文章主要為大家介紹了Angular懶加載模塊與Combined?Injector原理全面解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
使用ngView配合AngularJS應(yīng)用實現(xiàn)動畫效果的方法
這篇文章主要介紹了使用ngView配合AngularJS應(yīng)用實現(xiàn)動畫效果的方法,AngularJS是十分熱門的JavaScript庫,需要的朋友可以參考下2015-06-06
深入理解node exports和module.exports區(qū)別
下面小編就為大家?guī)硪黄钊肜斫鈔ode exports和module.exports區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
Angular限制input框輸入金額(是小數(shù)的話只保留兩位小數(shù)點)
最近做項目遇到這樣的需求輸入框要求輸入金額,只能輸入數(shù)字,可以是小數(shù),必須保留小數(shù)點后兩位。下面分為兩部分代碼給大家介紹實現(xiàn)代碼,需要的的朋友參考下吧2017-07-07

