0
該函數可以正常工作,但是當用戶輸入第一個數字到文本字段時,它將作爲空值傳遞給控制器,並且在第二個輸入之後它傳遞第一個輸入到控制器。我想用正確的值而不是空值爲第一次輸入的控制器賦值。對於號驗證angularjs自定義指令中的ng-change函數在用戶第二次輸入後更新輸入值
app.directive('faxFormat', ['$filter', function($filter) {
function link(scope, element, attributes) {
// scope.inputValue is the value of input element used in template
scope.inputValue = scope.phonenumberModel;
scope.$watch('inputValue', function(value, oldValue) {
value = String(value);
var number = value.replace(/[^0-9]+/g, '');
// scope.phonenumberModel = number;
scope.inputValue = $filter('phonenumber')(number);
scope.phonenumberModel = scope.inputValue;
});
scope.$watch('phonenumberModel', function(value, oldValue) {
scope.inputValue = scope.phonenumberModel;
});
// element.bind('click', function(){
// scope.method();
// });
scope.phoneFocused = function(){
scope.method();
}
}
return {
link: link,
//restrict: 'A',
//require: 'ngModel'
restrict: 'E',
scope: {
method: '&',
phonenumberPlaceholder: '=placeholder',
phonenumberModel: '=model',
myid: '=myid'
},
template: '<input ng-model="inputValue" type="tel" id="{{myid}}" class="phonenumber {{cls}}" ng-trim="false" placeholder="{{phonenumberPlaceholder}}" title="Phonenumber (Format:999-999-9999 or 1-999-999-9999)" ng-change="phoneFocused()">'
};
}]);
這裏
我已創建自定義的指令是我的控制器
$scope.removePhoneErrorHighlight = function() {
var fax =$scope.phoneData.phoneNumber;
console.log(fax);
}
這裏是我的html代碼
<fax-format class="faxFormat" myid="'accountPhone'" placeholder="'555-555-5555'" model="phoneData.phoneNumber" method="removePhoneErrorHighlight()" data-ng-focus="removePhoneErrorHighlight()"></fax-format>
我試過,但沒有得到知道在哪裏應用此函數並檢查它,如果我應用更改代碼給出錯誤 – Harry
附加您的Plunker代碼.. –