I'm having some problems with a directive that I want to be dynamically added as the user clicks a button. What would be the best approach to achieve this?
In my research I've found element.bind (from jqLite), but I haven't found lots of examples on the internet.
Here's my code:
HTML CODE
<div class="form-group">
<button class="btn btn-info" ng-click="addAttr()">Add Atributtes</button>
</div>
<addattributes options="attributes" attr="obj"></addattributes>
ANGULARJS DIRECTIVE
.directive('addattributes', function () {
return {
restrict: 'E',
scope: {
attributes: '=options',
attr: '='
},
template:
'<div ng-class="classInput">' +
' <div class="col-md-8" style="padding-left: 0;">' +
' <label>Nome do Atributo</label>' +
' <input type="text" class="form-control" placeholder="Attrs name" ng-model="attrname" ng-change="validation()">' +
' </div>' +
' <div class="col-md-4" style="padding-right: 0;"> ' +
' <label>Tipo do Atributo</label> ' +
' <select class="form-control" ng-options="attribute.name for attribute in attributes" ng-model="attrtype"></select>' +
' </div> '+
' <div class="clearfix"> '+
' <div> '+
' <button type="button" class="btn btn-default btn-xs pull-right" ng-click="changeButton()" style="margin-top: 1em;" ng-show="showBtn == true"> Adicionar </button> ' +
' </div> {{attr}}'+
'</div>',
link: function(scope,element,attrs){
scope.showBtn = true;
scope.classInput = 'form-group';
scope.attrtype= scope.attributes[2];
scope.changeButton = function(){
scope.showBtn = false;
scope.attr = {
name: scope.attrname,
type: scope.attrtype
};
}
scope.validation = function(){
if(scope.attrname.length < 6) {
scope.classInput = 'form-group has-error';
} else {
scope.classInput = 'form-group has-success';
}
}
}
};})
Thanks in advance, have a great day!
addAttr()function do?