I cannot call a function with ng-click

Asked

Viewed 6,836 times

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?

  • You need to use a $Compiler to load this dynamic html. I’ll build an example and already post

  • @In this case it would not be better to put the code in the html page and use ng-repeat?

  • It could also be @adelmo00. I also prefer this approach. I’m assuming that for some reason someone else doesn’t answer

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>
<script src="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>

  • It worked, thank you! = ) But explain to me, what is the use of $Compile?

  • $Compile is for you to bind elements that are in string format. In addition, it associates a specific $Scope for the content present in this string. Speaking more directly, it will make the angular understand the attributes that are present in this string. Among these attributes are click, change etc events...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.