I am working to a project with Ionic, AngularJs and PhoneGap. I want to build a directive which adds a button/icon to clean the text of an input.
I am aware that there are a lot of directives out there which do the same things, but I prefer to create it one from scratch. Also, I didn't find one that really satisfies my needs completely.
I didn't had no problem create the directive itself to append the icon, but I wasn't able to bind the click event. Seems like the event is completely ignored.
This is the directive.
(function() {
"use strict";
var app = angular.module("myApp");
app.directive("clearInput", function($compile) {
return {
require: 'ngModel',
link: function(scope, element, attrs) {
if (element.next().length) {
element.next().insertBefore(element);
}
var tpl = '<i class="icon ion-close-circled placeholder-icon clear-element" ng-show="' + attrs["ngModel"] + '.length>0" ></i>';
var clear = angular.element(tpl);
clear.bind('click', function(evt) {
//This never gets called :(
alert("You clicked me! Good for you.");
});
$compile(clear)(scope);
element.after(clear);
}
}
})
})();
I've also created a plunker to test it. http://plnkr.co/edit/KVqjpD?p=info
Any help is really appreciated!
Please, notice that I need to bind the click event to an element created at runtime, not to the element already present on the DOM.