【问题标题】:Angularjs watch when user type comma in input text当用户在输入文本中输入逗号时,Angularjs 会观察
【发布时间】:2016-04-14 11:31:23
【问题描述】:

一切都在问题中,我想在用户输入时观察输入文本,当他输入逗号时,输入得到验证而不是 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()" 是我忘记重新启动服务器。

【问题讨论】:

  • 创建非常简单的指令commaTyped

标签: javascript angularjs


【解决方案1】:

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

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

         action();

    }
});

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2016-08-10
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多