2
I’m learning Angularjs and I have a problem that I can’t solve.
I have a function inside a controller that’s inside a module. This function is responsible for mounting an accordion element with multiple items:
$scope.montaAccordion = function(dados){
var elemento = "";
for(var i=0; i < dados.length; i++){
elemento += "<div class='accordion-group' indice='collapse"+dados[i].AtributoVariacao.id+"'>";
elemento += "<div class='accordion-heading'>";
elemento += "<a class='accordion-toggle' data-toggle='collapse' data-parent='#sidebar-accordion' href='#collapse"+dados[i].AtributoVariacao.id+"'>";
elemento += "<i class='icon-reorder'></i>"+dados[i].Variacao.variacao+":: "+dados[i].AtributoVariacao.atributo;
elemento += "</a>";
elemento += "</div>";
elemento += "<a href='#' class='bt-del-atributo' style='float:right; margin: -33px 7px 0 0;'>";
elemento += "<i class='fa fa-times fa-2x'></i>";
elemento += "</a>";
elemento += "<div id='collapse"+dados[i].AtributoVariacao.id+"' class='accordion-body collapse'>";
elemento += "<div class='accordion-inner'>";
elemento += "<div class='control-group' data-fieldname='preco-diferenciado' style='padding:0;'>";
elemento += "<div class='controls'>";
elemento += "<label class='control-label'>Esta variação possui um preço diferenciado?</label>";
elemento += "<input type='text' name='preco-diferenciado-"+dados[i].AtributoVariacao.id+"' class='span12' value='' placeholder=''>";
elemento += "</div><br />";
elemento += "<button type='button' class='btn btn-info' ng-click='salvaPrecoDiferenciado("+dados[i].AtributoVariacao.id+")'>Salvar</button>";
elemento += "</div>";
elemento += "</div>";
elemento += "</div>";
elemento +="</div>";
}
$('#sidebar-accordion').html(elemento);
}
But in the stretch:
elemento += "<button type='button' class='btn btn-info' ng-click='salvaPrecoDiferenciado("+dados[i].AtributoVariacao.id+")'>Salvar</button>";
ng-click does not work. I believe there is a problem in the scope, or where I enter this function().
I declared the function on the same controller:
$scope.salvaPrecoDiferenciado = function(id){
alert(id);
}
and it didn’t work. Someone can help me?
When you click the button appears some error in the browser console?
– adelmo00
You need to use a $Compiler to load this dynamic html. I’ll build an example and already post
– Emir Marques
@In this case it would not be better to put the code in the html page and use ng-repeat?
– adelmo00
It could also be @adelmo00. I also prefer this approach. I’m assuming that for some reason someone else doesn’t answer
– Emir Marques