簡體   English   中英

當用戶在輸入文本中鍵入逗號時,Angularjs會監視

[英]Angularjs watch when user type comma in input text

一切都在問題中,我想在用戶輸入時查看輸入文本,當他輸入逗號時輸入得到驗證而不是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用於查找模型值的變化。 請參考這個 。所有$ watch都會在表達式發生變化時注冊回調。

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

         action();

    }
});

在你的情況下,你將不得不寫一個指令,尋找,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM