ng-model没有在safari浏览器中的命令粘贴上更新

时间:2017-06-28 10:30:26

标签: javascript angularjs

我有这个搜索文本框搜索文本。如果我逐个字符地添加文本它的工作正常,但是当我将数据直接粘贴到文本框中时,我的ng-model没有得到更新。此问题仅适用于 safari 浏览器

这是我的HTML

<div class="div-input pull-left" ng-submit="submitFilterForm()" ng-class="{ 'filter-dropdown-adjustment' : showFilterState() }">
            <input type="text" class="filter-input" ng-model="query" my-
     enter="submitFilterForm()" auto-focus />
        </div>

my-enter是我点击按回车键的自定义指令

我的指示

    'use strict';

  define([
'angular',
'./module',
      ], function(angular, directives) {

/**
 * Like ngShow but uses CSS visibility instead of display
 */
directives.directive('myEnter', [
    function() {
        return function(scope, element, attrs) {
            element.bind("keydown keypress", function(event) {
                if (event.which === 13) {
                    scope.$apply(function() {
                        scope.$eval(attrs.geEnter);
                    });
                    event.preventDefault();
                }
            });
        };
    }
])

});

这是我的js文件,其中包含用于文本框的ng-model上的$ watch函数

       $scope.$watch("query", function(name) {
            $scope.validationError = false;
            console.log("value of query is", name);
            filterStateService.updateSearchQuery(name);
        });

1 个答案:

答案 0 :(得分:0)

检查一下。我使用了ng-paste

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.angularjs.org/1.4.12/angular.js"></script>
  <script>
    var app = angular.module('myApp', []);
    app.controller('mainController', function($scope) {
    
    $scope.handlePaste =function(e){
      setTimeout(function(){
      $scope.query = e.target.value;
      console.log(e.target.value,"val")
     },10)
      
    }
    });
  </script>
</head>

<body ng-app="myApp">
  <div ng-controller="mainController">
    <input type="text" ng-paste="handlePaste($event)" ng-model="query" />
{{query}}

  </div>
</body>

</html>
&#13;
&#13;
&#13;