Angularjs观察用户在输入文本中键入逗号的情况

Angularjs watch when user type comma in input text

本文关键字:情况 用户 观察 输入 文本 Angularjs      更新时间:2024-06-17

所有这些都是问题所在,我想在用户键入时注意输入文本,当他键入逗号时,输入会得到验证,而不是ng-click="action()"我想要像Comma Typed="action()"这样的东西,我尝试了ng-change,scope.watch(),没有缺少。。

到目前为止:

<input id="tagInsert" type="text" name="newTag" ng-model="newTag" ng-model-options="{debounce: 100}" typeahead="tag for tag in getTags($viewValue)" class="form-control" typeahead-loading="loadingTags" ng-keydown="addInterestOnEvent($event)" ng-disabled="interestLimit" autocomplete="off" ng-change="alert('stuff')" />

控制器:

$scope.$watch('newTag', function(val) {
             if (val.keyCode == 188) { // KeyCode For comma is 188
             alert('comma added');
             action();
           }
    });

我也尝试过ng keydown:

$scope.addInterestOnEvent = function (event) {
        if (event.which !== 188) return;
        event.preventDefault();
        $scope.addInterest();
    };

这是行不通的。

编辑

它确实适用于ng-keydown="action()",只是我忘记了重新启动服务器。

您的$watch逻辑不正确$手表用于查找模型值的变化。请参考这个。$watch所做的只是在表达式更改时注册回调。

$scope.$watch('newTag', function(nv,ov) {
       if (nv!==ov ){
         action();
    }
});

在您的情况下,您必须编写一个指令并查找,

相关文章: