Angularjs input field focus event?

前端 未结 3 884
心在旅途
心在旅途 2021-01-03 18:02

i use angularjs and i have created a normal input field like this:


i

3条回答
  •  余生分开走
    2021-01-03 18:42

    If you are using functionality that you may wish to apply to fields throughout your application, you could put the it into a directive. Here is an example that adds and removes a css class based on the focus or blur of a field:

    angular.module('myApp').directive('inputFocus', function () {
    
    var FOCUS_CLASS = 'input-focused';
    return {
      restrict: 'A',
      priority: 1,
      require: 'ngModel',
      link: function (scope, element, attrs, ctrl) {
        element.bind('focus',function () {
          element.parent().addClass(FOCUS_CLASS);    
        }).bind('blur', function () {
          element.parent().removeClass(FOCUS_CLASS);
        });
      }
    };
    });
    

提交回复
热议问题