Angular.js: Set CSS when Input is on Focus

前端 未结 3 1927
臣服心动
臣服心动 2020-12-19 02:52

Does someone knows how to get the following working:

If an user clicks inside \"name\" - Set CSS Class to XYZ on DIV ?

E
3条回答
  •  温柔的废话
    2020-12-19 03:04

    If you're using Angular 1.2.x, see ng-focus and ng-blur:

    Enter your name here

    If you're using a 1.0.x version, nothing is stopping you from defining your own focus and blur directives based on Angular 1.2.x's:

    /*
     * A directive that allows creation of custom onclick handlers that are defined as angular
     * expressions and are compiled and executed within the current scope.
     *
     * Events that are handled via these handler are always configured not to propagate further.
     */
    var ngEventDirectives = {};
    forEach(
      'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress submit focus blur copy cut paste'.split(' '),
      function(name) {
        var directiveName = directiveNormalize('ng-' + name);
        ngEventDirectives[directiveName] = ['$parse', function($parse) {
          return function(scope, element, attr) {
            var fn = $parse(attr[directiveName]);
            element.on(lowercase(name), function(event) {
              scope.$apply(function() {
                fn(scope, {$event:event});
              });
            });
          };
        }];
      }
    );
    

提交回复
热议问题