$attrs undefined angular 用于比较密码和密码重复的指令

$attrs undefined angular directive for comparing password and password repeat

我正在学习均值堆栈开发并遵循视频教程课程。一切都很顺利,直到我在尝试匹配两个密码字段时遇到此错误。 “密码”和“确认密码” 我正在编写与教程完全相同的代码,但我在命令行中遇到以下错误 (Gulp serve)

error "$attrs" is not defind no-undef

error "$attrs" is not defind no-undef

这是代码

export function CompareToDirective($parse) {
'ngInject'
return {
    require: 'ngModel',
    link: function (scope, elm, attrs, ngModel) {
        var mainModel = $parse(attrs.compareTo);
        var secondModel = $parse(attrs.ngModel);

        scope.$watch(attrs.ngModel, function (newValue) {
            ngModel.$setValidity($attrs.name, newValue === mainModel(scope));
        });

        scope.$watch(attrs.compareTo, function (newValue) {
            ngModel.$setValidity($attrs.name, newValue === secondModel(scope));
        });
    }
}

}

编辑

这是我尝试验证的表格以防万一

<form name="register">
                <div class="form-group">
                    <label>Email address</label>
                    <input type="email" class="form-control">
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" class="form-control" name="pwd" ng-model="pwd">
                </div>
                <div class="form-group">
                    <label>Password Confirm</label>
                    <input type="password" class="form-control" compareTo="pwd" name="pwdConfirm" ng-model="pwdConfirm">
                </div>
                <span ng-show="register.pwdConfirm.$invalid">Passwords do not match</span>
                <button type="submit" class="btn btn-default">Submit</button>
            </form>

好的。我能够让它工作。请找到下面的代码

.directive("compareto", function($parse) {
  return {
  require: 'ngModel',
  link: function (scope, elm, attrs, ngModel) {
    var mainModel = $parse(attrs.compareto);
    var secondModel = $parse(attrs.ngModel);

    scope.$watch(attrs.ngModel, function (newValue) {
          ngModel.$setValidity(attrs.name, newValue === mainModel(scope));
    });

    scope.$watch(attrs.compareto, function (newValue) {
        ngModel.$setValidity(attrs.name, newValue === secondModel(scope));
    });
}
}
});

表格将是

<form name="register">
            <div class="form-group">
                <label>Email address</label>
                <input type="email" class="form-control">
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" class="form-control" name="pwd" ng-model="pwd">
            </div>
            <div class="form-group">
                <label>Password Confirm</label>
                <input type="password" class="form-control" compareto="pwd" name="pwdConfirm" ng-model="pwdConfirm">
            </div>
            <span ng-show="register.pwdConfirm.$invalid">Passwords do not match</span>
            <button type="submit" class="btn btn-default">Submit</button>
        </form>