国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Angular 常用指令實例總結整理

2019-11-19 18:31:36
字體:
來源:轉載
供稿:網友

Angular 常用指令

已經用了angular很久積累了一些很實用的指令,需要的話直接拿走用,有問題大家一起交流

1.focus時,input:text內容全選

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指令,外部點擊時觸發,click-outside="func()" func為自己指定的方法,一般為關閉當前層的方法,inside-id="" 點擊指定id的輸入框時,當前層不關閉

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指令,內部點擊時觸發

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 指令 ,內部滾動時觸發

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指令,內部獲得焦點或者點擊時觸發

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 作為一個元素的屬性存在:如果監聽的表達式值為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 滾動到頂部的時候觸發

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 作為一個元素的屬性存在:如果監聽的表達式值為true,則將本元素中所綁定的ngModel值設為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 作為一個元素的屬性存在:如果監聽的表達式值為true,則將本元素中所綁定的ngModel值設為空字符串。

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 輸入框內容僅限數值的指令(輸入內容不允許為 負值),可以設定最大值(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 輸入框自動轉換成大寫

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 輸入框內容不能為特殊字符

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 輸入框失去焦點 保留兩位小數

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編譯期設置元素高度,可以接受數字或者表達式

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);      }    }  };}]); 

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴东县| 杂多县| 沁阳市| 公主岭市| 依兰县| 蕲春县| 鄂托克旗| 香河县| 乌海市| 祁连县| 建湖县| 海林市| 尚志市| 紫阳县| 鄯善县| 正安县| 四川省| 乐山市| 枞阳县| 巴青县| 库伦旗| 江西省| 达尔| 阿拉尔市| 吴堡县| 榆林市| 茌平县| 绥宁县| 湖南省| 无为县| 清徐县| 石景山区| 德清县| 普陀区| 云阳县| 孙吴县| 远安县| 海口市| 临洮县| 临洮县| 江门市|