Javascript 角度自定义验证器导致范围变量未定义?

Javascript 角度自定义验证器导致范围变量未定义?,javascript,angularjs,validation,Javascript,Angularjs,Validation,我正在为检查数组长度的指令实现一个自定义验证器,但当我尝试取消注释验证器中的一行代码时,我遇到了一个有趣的问题: angular.module("directives.tags", []).directive("tags", ["Tag", "$timeout", function(Tag, $timeout) { return { require: 'ngModel', restrict: 'E', scope: {

我正在为检查数组长度的指令实现一个自定义验证器,但当我尝试取消注释验证器中的一行代码时,我遇到了一个有趣的问题:

angular.module("directives.tags", []).directive("tags", ["Tag", "$timeout", function(Tag, $timeout) {
    return {
        require: 'ngModel',
        restrict: 'E',
        scope: {
            availableTags: '=',
            currentTags: '=ngModel'
        },
        link: function($scope, element, attributes, ctrl) {

            // Snip

            $scope.updateSuggestionList = function() {
                var search = new RegExp($scope.tagInput, "i");

                $scope.suggestions = $scope.availableTags.filter(function(availableTag) {
                    if ($scope.currentTags.filter(function(currentTag) {
                            return availableTag.name == currentTag.name;
                        }).length == 0) {
                        return search.test(availableTag.name);
                    }
                    return false;
                }).slice(0,6);
            };

            // PROBLEM APPEARS TO BE HERE
            ctrl.$validators.taglength = function(currentTags) {
                return true; // <-- This works just fine
                return currentTags.length > 0 && currentTags.length < 6; // <-- This does not work at all
            };

            $scope.$watch('currentTags', function() {
                ctrl.$validate();
            }, true);

        },
        templateUrl: // snip
    }
}]);

解决了。在
$validate()
的文档中:

运行每个注册的验证器(首先是同步验证器,然后是异步验证器)。如果有效性更改为无效,则模型将设置为未定义,除非ngModelOptions.allowInvalid为true


所以我只需要设置
ngModelOptions.allowInvalid=true

已解决。在
$validate()
的文档中:

运行每个注册的验证器(首先是同步验证器,然后是异步验证器)。如果有效性更改为无效,则模型将设置为未定义,除非ngModelOptions.allowInvalid为true


所以我只需要设置
ngModelOptions.allowInvalid=true

您不需要将作用域绑定到
ng model
属性(事实上,您不应该这样做),因为
需要:“ngModel”
-要访问该值,请使用
ctrl.$modelValue
(在第一个摘要周期中它可能是
未定义的
)何时调用
updateSuggestionList
以及如何更新
currentTags
?在
updateSuggestionList
中抛出错误。无法理解验证器是如何影响它的!请同时发布触发
updateSuggestionList
functioPreethi的代码。我添加了指令模板。@NewDev,因此我尝试使用
ctrl.$modelValue
,得到了相同的结果-它是空的,然后几乎是瞬间未定义的。您能告诉我为什么不将作用域绑定到
ng model
?您不需要将作用域绑定到
ng model
属性(事实上,您不应该这样做),因为
需要:“ngModel”
-要访问该值,请使用
ctrl.$modelValue
(在第一个摘要周期中它可能是
未定义的
)何时调用
updateSuggestionList
以及如何更新
currentTags
?在
updateSuggestionList
中抛出错误。无法理解验证器是如何影响它的!请同时发布触发
updateSuggestionList
functioPreethi的代码。我添加了指令模板。@NewDev,因此我尝试使用
ctrl.$modelValue
,得到了相同的结果-它是空的,然后几乎是瞬间未定义的。您能告诉我为什么不将作用域绑定到
ng model
Error: $scope.currentTags is undefined
.link/$scope.updateSuggestionList/$scope.suggestions<@http://localhost:3000/js/app.js:1116:1
.link/$scope.updateSuggestionList@http://localhost:3000/js/app.js:1115:38
anonymous/fn@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js line 13231 > Function:2:248
ngEventHandler/</callback@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:23411:17
$RootScopeProvider/this.$get</Scope.prototype.$eval@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:15916:16
$RootScopeProvider/this.$get</Scope.prototype.$apply@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:16016:20
ngEventHandler/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:23416:17
n.event.dispatch@https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js:3:6414
n.event.add/r.handle@https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js:3:3224
<div class="tagger-container">
    <div class="wrapper" ng-class="{ 'active': areSuggestionsVisible }">
        <div class="tag-wrapper">
            <div class="tag" ng-repeat="tag in currentTags">
                [[ tag.name ]]
                <span class="remove" ng-click="removeTag(tag)"></span>
            </div>
        </div>
        <input type="text" class="tag-input"
               ng-model="tagInput"
               ng-style="{ width: inputLength + 'px'}"
               ng-keydown="tagInputKeydown($event)"
               ng-keyup="updateSuggestionList()"
               ng-focus="toggleSuggestionVisibility()"
               ng-blur="toggleSuggestionVisibility()" />
    </div>
    <div class="suggestions" ng-show="areSuggestionsVisible">
        <div class="suggestion" ng-repeat="tag in suggestions" ng-mousedown="createTag(tag.name)">[[ tag.name ]] </div>
    </div>
</div>
<tags available-tags="data.tags" name="tags" ng-model="text.tags"></tags>
[{ name: 'aTag', description: null, id: 1}, ..., ...]