javascript - angular directive getting $parse not defined - Stack Overflow

I have a js or angular problem. I don't know why I'm getting $parse isnt defined when I'

I have a js or angular problem. I don't know why I'm getting $parse isnt defined when I'm running this:

function link(scope, elem, attrs, ctrl) {
    scope.$watch(function() {
        var valid = $parse(attrs.fieldMatch)(scope) === ctrl.$modelValue;
        ctrl.$setValidity('mismatch', valid);
    });
}

function fieldMatch($parse) {
    return {
        restrict:'AE',
        require: 'ngModel',
        link: link
    }
}

angular.module('fieldMatch', [])
    .directive('fieldMatch', fieldMatch);

I have a js or angular problem. I don't know why I'm getting $parse isnt defined when I'm running this:

function link(scope, elem, attrs, ctrl) {
    scope.$watch(function() {
        var valid = $parse(attrs.fieldMatch)(scope) === ctrl.$modelValue;
        ctrl.$setValidity('mismatch', valid);
    });
}

function fieldMatch($parse) {
    return {
        restrict:'AE',
        require: 'ngModel',
        link: link
    }
}

angular.module('fieldMatch', [])
    .directive('fieldMatch', fieldMatch);
Share Improve this question edited Oct 9, 2015 at 16:49 LionC 3,1061 gold badge23 silver badges31 bronze badges asked Oct 30, 2014 at 14:38 user4198877user4198877
Add a ment  | 

1 Answer 1

Reset to default 5

The link function you defined is outside of the scope that would be created when calling fieldMatch(), thus, it has no visibility of $parse. Define it inside the directive's definition function, like so:

function fieldMatch($parse) {
  return {
    restrict:'AE',
    require: 'ngModel',
    link: link
  }

  function link(scope, elem, attrs, ctrl) {
    scope.$watch(function() {
      var valid = $parse(attrs.fieldMatch)(scope) === ctrl.$modelValue;
      ctrl.$setValidity('mismatch', valid);
  });
}

angular.module('fieldMatch', [])
  .directive('fieldMatch', fieldMatch);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745341145a4623331.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信