【发布时间】:2017-08-11 12:06:51
【问题描述】:
当我的控制器中满足某些条件时,我正在尝试向浮动占位符标签添加错误
但是,我不确定解决此问题的最佳方法,并且我当前的实现似乎没有检测到指令中的属性更改(自定义错误保持设置为“测试”)。
这是我现在得到的:
HTML:
<input type="password" float-placeholder
custom-error="test" placeholder="Confirm password"
required name="passwordSecond" id="passwordSecond"
ng-model="vs.PasswordSecond" />
指令:
angular.module('myApp').directive('floatPlaceholder', function ($window) {
return {
restrict: 'A',
scope: {
customError: '@'
},
link: function (scope, element, attrs) {
element.after("<label class='floating-placeholder'>" + attrs.placeholder + "</label>");
var label = angular.element(ele.parent()[0].getElementsByClassName('floating-placeholder'));
element.on('blur', function() {
if (ele.val().length > 0) {
if (scope.customError) {
label.text(attrs.placeholder + ' - ' + scope.customError);
}
}
}
}
};
});
控制器:
angular.module('myApp').controller('SignupController', function factory() {
_this.confirmPassword = () => {
if(_this.PasswordFirst !== _this.PasswordSecond){
angular.element(signupForm.passwordSecond).attr('custom-error', _this.Error);
}
}
});
我正在使用 Angular 1.6
【问题讨论】:
-
代码是位的,请提供一个突出问题的工作演示。我正在尝试重新创建,但缺少一些变量
标签: angularjs angularjs-directive angularjs-ng-model angularjs-ng-form