In my AngularJS application, I am faced with a variety of complex inputs scattered throughout. Some inputs utilize directives for autocompletion functionality, like those powered by Google Places or Twitter Bootstrap.
I am currently exploring the concept of creating a directive that would mimic the iOS feature of displaying an erase button when text is entered.
Although I attempted to implement this directive, I encountered issues with the scope.erase
not initializing, as well as the ng-show
not functioning as expected.
Is it feasible to dynamically add HTML elements after the text input and manipulate them within the controller?
Here is the directive I experimented with:
app.directive('eraseBtn', function($parse, $compile){
return {
require: 'ngModel',
restrict: "A",
transclude: true,
link : function(scope, element, attrs, model){
element.parent().append('<button ng-click="erase()" ng-show="model.length > 0" class="erase-btn">x</button>');
scope.erase = function(){
console.log('Erase test');
}
}
}
});
I am reluctant to utilize a template since the HTML structure of my inputs varies significantly.