Angular 常用指令實(shí)例總結(jié)整理
Angular 常用指令
已經(jīng)用了angular很久積累了一些很實(shí)用的指令,需要的話直接拿走用,有問題大家一起交流
1.focus時(shí),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指令,外部點(diǎn)擊時(shí)觸發(fā),click-outside="func()" func為自己指定的方法,一般為關(guān)閉當(dāng)前層的方法,inside-id="" 點(diǎn)擊指定id的輸入框時(shí),當(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)部點(diǎn)擊時(shí)觸發(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)部滾動(dòng)時(shí)觸發(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)部獲得焦點(diǎn)或者點(diǎn)擊時(shí)觸發(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 使元素可拖動(dòng)
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 作為一個(gè)元素的屬性存在:如果監(jiān)聽的表達(dá)式值為true,則將焦點(diǎn)放到本元素上。
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 滾動(dòng)到頂部的時(shí)候觸發(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 作為一個(gè)元素的屬性存在:如果監(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 作為一個(gè)元素的屬性存在:如果監(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 輸入框自動(dòng)轉(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 輸入框失去焦點(diǎn) 保留兩位小數(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);
}
}
};
}]);
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
angularjs封裝bootstrap時(shí)間插件datetimepicker
這篇文章主要介紹了angularjs封裝bootstrap時(shí)間插件datetimepicker 的相關(guān)資料,需要的朋友可以參考下2016-06-06
AngularJS學(xué)習(xí)第二篇 AngularJS依賴注入
這篇文章主要為大家詳細(xì)介紹了AngularJS學(xué)習(xí)第二篇,理解什么是AngularJS依賴注入,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
angular.js實(shí)現(xiàn)列表orderby排序的方法
今天小編就為大家分享一篇angular.js實(shí)現(xiàn)列表orderby排序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
AngularJS入門教程之?dāng)?shù)據(jù)綁定原理詳解
這篇文章主要介紹了AngularJS數(shù)據(jù)綁定原理,較為詳細(xì)的分析了AngularJS數(shù)據(jù)綁定的原理、使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-11-11
詳解什么是@ngrx/store開發(fā)包中的MemoizedSelector
這篇文章主要為大家介紹了@ngrx/store開發(fā)包中的MemoizedSelector使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Angular搜索場(chǎng)景中使用rxjs的操作符處理思路
這篇文章主要介紹了Angular搜索場(chǎng)景中使用rxjs的操作符處理思路,主要的思路就是通過Subject來發(fā)送過濾條件,這樣就可以使用rxjs的各種操作符,可以快捷很多。需要的朋友可以參考下2018-05-05
AngularJS實(shí)現(xiàn)用戶登錄狀態(tài)判斷的方法(Model添加攔截過濾器,路由增加限制)
這篇文章主要介紹了AngularJS實(shí)現(xiàn)用戶登錄狀態(tài)判斷的方法,通過Model添加攔截過濾器,路由增加限制實(shí)現(xiàn)針對(duì)登陸狀態(tài)的判斷功能,需要的朋友可以參考下2016-12-12

