我以爲我解決了這個問題,但是我錯了..... 我創建了一個指令,允許我清除文本輸入。基本上,當您開始在輸入字段中輸入時,文本框右側會顯示經典的「X」圖標。當你點擊它時,模型被刪除。 這是我的指令。將單擊事件綁定到在指令中創建的動態元素
(function() {
"use strict";
var app = angular.module("myApp");
app.directive("clearInput", ['$compile', function ($compile) {
return {
require: 'ngModel',
scope: {},
link: function (scope, element, attrs, ngModel) {
if (element.next().length) {
element.next().insertBefore(element);
}
var tpl = '<span> <i class="icon ion-close-circled placeholder-icon clear-element" ng-show="show" ></i></span>';
var clear = angular.element(tpl);
scope.setValue = function (val) {
ngModel.$setViewValue(val);
ngModel.$render();
scope.$apply();
};
clear.on('click',
function() {
scope.setValue(null);
});
element.bind('blur', function() { scope.setValue(ngModel.$modelValue); });
scope.$watch(function() {
return ngModel.$modelValue;
}, function (val) {
scope.show = val === null ? null : val.length > 0;
});
$compile(clear)(scope);
element.after(clear);
}
}
}]);
})();
現在,當我創建並測試指令,我做到了使用plunker,我包括了錯誤的很老的版本離子(1.0.0-beta.5)。在這種情況下,該指令就像一個魅力。
http://plnkr.co/edit/5rGzl1?p=info
當我移動的指令轉化爲我的申請,我發現我綁定Click事件不會fire.So我分叉的plunker和我用了一個更新的版本離子(1.0.1),和在這種情況下,點擊不起作用(但是dblclick確實.doh!)。
http://plnkr.co/edit/DH6jjG?p=info
有誰知道如何解決呢? 謝謝!
好的。我其實不明白爲什麼會發生這種情況。至少我現在有一個解決方法。 – MilitelloVinx