I can not call a function with ng-click

2

I'm learning AngularJS and I have a problem that I can not solve.

I have a function inside a controller that is inside a module. This function is responsible for assembling an accordion element with several 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 excerpt:

elemento += "<button type='button' class='btn btn-info' ng-click='salvaPrecoDiferenciado("+dados[i].AtributoVariacao.id+")'>Salvar</button>";

ng-click does not work. I think it's a problem in the scope, or where I enter this DiffPrev () function.

I declare the function in the same controller:

$scope.salvaPrecoDiferenciado = function(id){
     alert(id);
}

and did not work. Can anyone help me?

    
asked by anonymous 03.11.2015 / 16:42

1 answer

2

You need to use $ compile. See the example below:

var app = angular.module("App", []);

app.controller("DemoController", function($scope, $compile){
    $scope.montaAccordion = function(){
        var elemento = "";
        for(var i=0; i < 5; i++){
            elemento += "<input type='button' ng-click='clickItem()' value='Teste: " + i + "'>";
        }
        $('#sidebar-accordion').html($compile(elemento)($scope));
    };
    
    $scope.clickItem = function(){
        console.log("Teste...");
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div ng-app="App" ng-controller="DemoController" ng-init="montaAccordion()">
  <div id="sidebar-accordion" />
</div>
    
03.11.2015 / 17:21