[英]Changing the input after focus is removed from directive in angular js
我只想在焦點從特定元素中刪除后更改輸入(僅使用指令),我不想使用ng-focus
和ng-blur
例如-在這里,當焦點從以下位置丟失時,我想將文本更改為大寫文本框我有一個文本框
<input type = "text" ng-model = "text" upper>
這是我的指令
app.directive('upper', function () {
return {
require: 'ngModel',
link: function (scope, element, attr, ngModelCtrl) {
function fromUser(text) {
alert(text);
var transFormetInput = text;
transFormetInput = transFormetInput.toUpperCase();
alert(transFormetInput);
return transFormetInput;
}
ngModelCtrl.$parsers.push(fromUser);
}
};
});
現在,在這里,我的$ parser會在模型中輸入的每個單詞都改變我的輸出,即在改變范圍后,每個字母都會被激發,我想改變模型。
這是我的朋克
這個pl子有兩個問題。
1)由於我在這里使用了$ parser,它在每個我不想輸入的單詞上觸發,當我的文本框失去焦點時,我希望它觸發。
2)我的模型沒有改變我的指令。
我會嘗試保存您的插件,但我沒有權限,因此,請像這樣使用。
app.directive('upper', function() {
return {
require: 'ngModel',
link: function(scope, element, attr) {
element.on('blur', function() {
element[0].value = element[0].value.toUpperCase();
});
}
};
});
如果要模糊,請嘗試以下操作:
link: (scope, el, attr, ctrl) => {
el.bind('blur', () => {
el.val(ctrl.yourInput.value);
});
}
您想要的值應該在控制器/模型上(鏈接中的第4個arg)。 我建議每次更新時將其分配給變量,然后您可以在el.bind('blur')方法中進行訪問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.