2
I am trying to create a Directive that has its Scope isolated, but that has some functions that she will be responsible for doing p/Directive. It is a div that contains a table and an "Add" button when clicking the add will open below the table a form type to feed that table. I used transclude p/ this form and would like to click on a certain button (included in transclude) come execute the functions I determined in Directive p/ copy the records to table dynamically, so it doesn’t come to every controller that uses this directive I have to do everything in hand this feature. Follow the codes below:
HTML Directive:
<div class="form-horizontal panel panel-default panel-group">
<div class="panel-heading">
<div class="form-group">
<div ui-grid="options" class="grid" />
</div>
</div>
<div class="panel-footer" style="margin-top: -25px;">
<div style="text-align: right;">
<button type="button" class="btn btn-success" data-toggle="collapse" data-target="#divForm{{idGrid}}">Adicionar</button>
</div>
<div id="divForm{{idGrid}}" class="collapse">
<br>
<ng-transclude></ng-transclude>
</div>
</div>
</div>
grid-form.directive.js :
(function() {
'use strict';
angular
.module('controlwebApp')
.directive('gridForm', gridForm);
function gridForm () {
var directive = {
restrict: 'E',
transclude: true,
scope: {
options: '=',
idGrid: "@idGrid"
},
templateUrl: 'app/components/grid/gridform/grid-form.directive.html',
bindToController: {
teste: '&'
},
controller: function (){
this.teste = function(){
console.log("Teste Calling!!")
}
},
controllerAs: 'ctrl'
};
return directive;
}
})();
Use of the directive:
<div class="form-group">
<grid-form id-grid="grid1" options="vm.gridOptions">
<div class="form-horizontal">
<label class="control-label" for="field_qtdEstoqueMax">Qtd Estoque Max1</label>
<input type="number" class="form-control" name="qtdEstoqueMax" id="field_qtdEstoqueMax" ng-model="vm.produto.qtdEstoqueMax" />
<label class="control-label" for="field_qtdEstoqueMax">Qtd Estoque Max2</label>
<input type="number" class="form-control" name="qtdEstoqueMax" id="field_qtdEstoqueMax" ng-model="vm.produto.qtdEstoqueMax"/>
<button type="button" class="btn btn-primary" ng-click="ctrl.teste()">Save me</button>
</div>
</grid-form>
</div>
Perfect strategy brother, worked correctly. Thank you, unfortunately I do not have enough points to give +1 in your answer =( I had a little problem in my controller of the directive, an error (https://docs.angularjs.org/error/$injector/strictdi) that already appears in doc. from angular to normal Controller correction, I solved like this: controller: ['$Scope', Function($Scope) { $Scope.gridBindObject.onteste = Function(){ Alert("Worked"); } }]
– Raul Marques
@Raulmarques Don’t stress about it, I’m glad it worked. If so, mark as accepted answer if your problem has been solved. =)
– OnoSendai