【发布时间】:2014-01-31 19:46:01
【问题描述】:
考虑以下示例:
<input type="text" ng-model="client.phoneNumber" phone-number>
<button ng-click="doSomething()">Do Something</button>
.directive("phoneNumber", function($compile) {
return {
restrict: 'A',
scope: true,
link: function(scope, element, attrs) {
scope.mobileNumberIsValid = true;
var errorTemplate = "<span ng-show='!mobileNumberIsValid'>Error</span>";
element.after($compile(errorTemplate)(scope)).on('blur', function() {
scope.$apply(function() {
scope.mobileNumberIsValid = /^\d*$/.test(element.val());
});
});
}
};
});
查看demo,如果您在电话号码末尾添加say 'a',然后单击按钮,则不会呼叫doSomething()。如果再次单击该按钮,则会调用 doSomething()。
为什么第一次没有调用doSomething()?任何想法如何解决这个问题?
注意:保持验证模糊很重要。
【问题讨论】:
-
这个怎么样?
#wrapper {width: 220px;}。我认为最好保持你的设计保持原样,尽管动态因素,即错误消息,被添加和删除。不过这是你的电话。 -
在使用按钮的同时移动按钮是非常糟糕的 UI 设计。当出现错误时,我会将跨度留在那里并将空白 (
&nbsp;) 文本更改为“错误”。
标签: angularjs angularjs-directive