search

Home  >  Q&A  >  body text

javascript - angularjs ngblur does not take effect but onblur takes effect, why?

<input class="detail-reply"
        id="replyInput"
        type="text"
        ng-model="$ctrl.replyString">

This input will not be triggered using ng-blur, but the direct DOM binding onblur event will be triggered. Why?

淡淡烟草味淡淡烟草味2815 days ago860

reply all(2)I'll reply

  • 世界只因有你

    世界只因有你2017-06-30 09:59:53

    Are you using 1 or 2? If it is 2, you can
    <input type="text" [(ngModel)]="myModel" (blur)="onBlurMethod()">

    export class AppComponent { 
      myModel: any;
      constructor(){
        this.myModel = '123';
      }
      onBlurMethod(){
       alert(this.myModel) 
      }
    }

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-30 09:59:53

    angular1’s ng-blur can only be used through instructions. The function of the instructions is actually to apply the event bound by ng-blur to the onblur event

    app.directive('ngBlur', ['$parse', function($parse) {
      return function(scope, element, attr) {
        var fn = $parse(attr['ngBlur']);
        element.bind('blur', function(event) {
          scope.$apply(function() {
            fn(scope, {$event:event});
          });
        });
      }
    }]);

    reply
    0
  • Cancelreply