ng节目没有't从指令中捕获更新的$invalid值,原因是什么
ng-show doesn't catch the updated value of $invalid from the directive, why?
问题是:当我聚焦于一个字段并模糊它时,它会向我显示错误消息和输入字段周围的边框颜色,然后我再次聚焦它,写0并模糊它,它仍然向我显示输入的红色边框,但不显示错误消息。
该字段应包含一个大于1.00的值,但当我加载页面时,默认值为0.00,但如果在模糊字段时触摸/聚焦字段,则会出现错误。
HTML:
<div class="col-xs-12 a24sm-5">
<input class="form-control" gh-amount-field name="singleLimit" id="singleLimit" ng-model="crCtrl.form.limits.single.val" ng-blur="crCtrl.form.limits.single.errFn()" tabindex="7" />
<span class="visible-xs msg-container msg-single">
<span class="gh-error" ng-show="cardRules.singleLimit.$invalid">{{crCtrl.form.requiredMsg}}</span>
</span>
</div>
指令是:
directive("ghAmountField", function(){
return{
require: "^form",
link: function(s, el, attrs, formCtrl){
var initialAmount;
el.on("blur", function(){
var min = attrs.hasOwnProperty("amountMin") ? parseFloat(attrs.amountMin) : false;
var max = attrs.hasOwnProperty("amountMax") ? parseFloat(attrs.amountMax) : false;
var req = attrs.hasOwnProperty("required");
var amount = $(this).val().length > 0 ? $(this).val().replace(/[^'d'.]/g, "") : initialAmount.replace(/[^'d'.]/g, "");
if( amount.length>0) {
amount = parseFloat(amount).toFixed(2);
$(this).val("£"+amount);
}
else{
$(this).val("£"+initialAmount);
amount = initialAmount;
}
formCtrl[attrs.name].$invalid = false;
if(!req && amount<1){//after first changing you cannot put zero but at least 1.00
$(this).val("£"+initialAmount);
amount = initialAmount;
formCtrl[attrs.name].$invalid = initialAmount<1 ? true : false;
}
console.log(amount, initialAmount);
if (!formCtrl[attrs.name].$invalid && ( typeof min == 'number' && amount < min || typeof max == 'number' && amount > max )) {
formCtrl[attrs.name].$invalid = true;
}
console.log(amount, initialAmount);
if (formCtrl[attrs.name].$invalid) {
//$(el).removeClass(formCtrl[attrs.name].$invalid ? "ng-valid" : "ng-invalid").addClass(formCtrl[attrs.name].$invalid ? "ng-valid" : "ng-invalid");
$(this).removeClass("ng-valid").addClass("ng-invalid");
}
else {
$(this).removeClass("ng-invalid").addClass("ng-valid");
}
})
.on("focus", function(){
initialAmount = $(this).val().replace(/[^'d'.]/g, "");
$(this).val("");
})
.on("keypress", function(){
$(this).removeClass("ng-invalid").addClass("ng-valid");
formCtrl[attrs.name].$invalid = false;
});
}
}
});
任何在上下文之外更新angular scope/binding的东西,angular都不知道。为了更新绑定,我们需要手动运行摘要循环,这最终会告诉angular绑定已经更改,并根据它更新绑定。
对于手动运行摘要循环,您可以在范围内使用$apply()
/$timeout
/$applyAsync
方法来查看效果。
也可以使用具有该元素的el
来代替$(this)
。
代码
.on("focus", function(){
$timeout(function(){
initialAmount = $(this).val().replace(/[^'d'.]/g, "");
el.val("");
})
})
.on("keypress", function(){
$timeout(function(){
el.removeClass("ng-invalid").addClass("ng-valid");
formCtrl[attrs.name].$invalid = false;
})
});
相关文章:
- Javascript循环不会自我更新
- 添加文字和评论功能更新Div
- AngularJS:ng之后,重复$scope值未按预期更新
- 如何通过数组更新角度子范围
- Ajax聊天消息重复而不仅仅是更新
- 通过CSV文件上载更新数据库表
- 平均值:无法将数据更新到数据库
- $rootScope未使用forEach进行更新
- d3基于用户选择动态更新节点
- 有条件更新d3.js力图中节点的最佳方法
- Angular:更新一次性绑定的数据
- Javascript更新孙窗口中的表单元素
- 使用AngularJS中的筛选器更新给定的表
- 从选项页面更新chrome扩展清单权限
- 如何在不刷新页面的情况下更新显示框
- 延期承诺值未更新/解析/延期
- 标记的实时更新,无需加载页面谷歌地图API V3
- 使用mongodb更新中的一个变量
- 访问jsrender模板中的全局javascript变量并更新它
- ng节目没有't从指令中捕获更新的$invalid值,原因是什么