【发布时间】:2023-04-03 05:26:01
【问题描述】:
我正在编写一个过滤器,它将在我建立的联系表单中格式化电话号码,但是由于某种原因,输入中的值永远不会更新,我不确定我做错了什么。
这是我的 HTML:
<div class='form-group'>
<input name='phone' ng-model='home.contact.phone' placeholder='(Area code) Phone' required ng-bind='home.contact.phone | phone' />
</div>
这是我的过滤器:
(function () {
'use strict'
angular
.module('Allay.phoneFilter', [])
.filter('phone', function () {
return function (phone) {
if(!phone) return '';
var res = phone + '::' // Since this isn't working, I'm doing something super simple, adding a double colon to the end of the phone number.
return res;
}
});
})();
我不确定你是否需要这个,但这是控制器:
(function () {
'use strict'
angular
.module('Allay', [
'Allay.phoneFilter'
])
.controller('HomeController', function () {
var home = this;
});
})();
如果我在过滤器中的 'return res' 之前添加一个 alert(res),我会看到我期望的值 '123::',但是输入中它自身的值仍然只是 123。
【问题讨论】:
-
我会在哪里做呢?我在文档中没有看到任何关于需要 $scope.$apply() 和过滤器的内容。
-
好的,由于您只是更改了问题的内容以使我的答案无效,因此我将假设其他答案之一会有所帮助。
标签: angularjs data-binding filter angular-ngmodel