我有一个js或角度问题。我不知道为什么在运行此命令时得到$parse isnt defined
:
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);
最佳答案
您定义的链接函数不在调用fieldMatch()
时将创建的范围之内,因此,它没有$parse
的可见性。在指令的定义函数中定义它,如下所示:
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);