如何从指令中的ng-model获得价值?

时间:2017-06-14 11:03:29

标签: angularjs

我有代码:

<input check-value-type-array type-value="node.type_value" type-element="node.type" ng-value="item" class="form-control">

指令:

.directive('checkValueTypeArray', function() {
         return {

             restrict: 'A',
             require: 'ngModel',
             scope: {
                 typeElement: '=',
                 typeValue: '=',
                 ngModel: '='
             },

             link: function(scope, element, attrs, ngModel) {


                 ngModel.$validators.required = function(v) {}

             }

如何获取ng-model inside指令并进行验证:

ngModel.$validators.required = function (v) {
}

1 个答案:

答案 0 :(得分:1)

轻微改动,你正在寻找ngModel。$ modelValue:

ngModel.$validators.required = (value) => {
  // the value of the model will be ngModel.$modelValue
}