Updating model after focus is lost on input control

后端 未结 2 798
小鲜肉
小鲜肉 2021-02-09 15:26

In this code:


AngularJS will update the model actionText as the user types stuff

相关标签:
2条回答
  • 2021-02-09 15:32

    I think the easiest way might be just use ng-blur to fire off something you want to do.

    http://plnkr.co/edit/rhcliQRzUOBKQ3xKFrde?p=preview

    app.controller('MainCtrl', function($scope) {
    
      $scope.myDataBlurred = $scope.myData;
    
      $scope.blurred = function() {
         $scope.myDataBlurred = $scope.myData;
      }
    });
    
     <input ng-model='myData' ng-blur='blurred()' />
    
        <div>
          This will update as you type: {{myData}}
        </div>
        <div>
          This will update after you blur:  {{myDataBlurred}}
        </div>
    
    0 讨论(0)
  • 2021-02-09 15:33

    You can use the ngModelOptions directive for this. With ngModelOptions you can further refine how the ngModel directive works. To achieve what you asked for, you may use it like this:

    <input class="form-control" ng-model="actionText" 
       ng-model-options="{ updateOn: 'blur'}"/>
    

    You can find further information and a working example in the angular documentation: https://docs.angularjs.org/api/ng/directive/ngModelOptions

    0 讨论(0)
提交回复
热议问题