最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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);
发布评论

评论列表(0)

  1. 暂无评论