How do I use a dynamic information coming from ng-repeat {{}} and use it as an argument to an ng-click (button) function?

2

I have a ng-click function:

ng-click = "adicionar(0);"

This function ng-click is inside the ng-repeat = "mercadoria in listademercadoria" loop.

Is there a way to pass information according to repeat where it is contained?

How I'm trying, unsuccessful:

ng-click = "adicionar({{mercadoria.mercadoria}});"

Follow AngularJS code:

angular.module('meumodulo', [])

    .controller('mercadoriaCarrinho', function($rootScope, $http) {
            var ctrl = this;
            $rootScope.listademercadoria = [];
           
            $rootScope.mercadoria0 = {
                id: 'id1',
                setor: 'setor1',
                foto: 'foto1',
                descr: 'descr1',
                de: de1,
                por:por1,
                mercadoria: '0',
                quantidade: 1
            }

            $rootScope.listademercadoria.push($rootScope.mercadoria1);
            $rootScope.mercadoria1 = {
                id: 'id2',
                setor: 'setor2',
                foto: 'foto2',
                descr: 'descricao2',
                de: de2,
                por: por2,
                mercadoria: '1',
                quantidade: 1
            }
            
            $rootScope.adicionar = function (a){
					{
					$rootScope.listademercadoria[a].quantidade=$rootScope.listademercadoria[a].quantidade + 1;
					}						
				}
									
			});

HTML button that would perform the action of adding one to the quantity in the merchandise with index where it is contained:

<button ng-click = "adicionar({{mercadoria.mercadoria}});"> + </button>

I emphasize that functions of another type, other buttons parallel to it, are working correctly, but only in this function I can not make it work correctly.

    
asked by anonymous 22.09.2017 / 20:55

2 answers

3

You do not need to {{}} just remove it.

example:

<div class="card" ng-click="goToListaDeRespostas($index)"  ng-repeat="item in lista | orderBy:'ordem'">

or

<button ng-click = "adicionar(mercadoria.mercadoria)"> + </button>
    
22.09.2017 / 20:59
2

The CCastro response is correct - though I think it's worth an explanation of which cases need the markers, and which ones do not:

  • Double keys ( {{}} ) are used to mark an angle expression. You use them when you want to insert content rendered by Angular directly into the HTML. Example:
    <div>{{conteudo}}</sdiv>

  • No bullets : When you are using the property directly in a unique Angular method: Example:
    <a href='#' ng-click="salvar(conteudo)">{{conteudo}}</sdiv>

  • Simple Keys : When you want to express a javascript object within a native expression:
    <a href='#' ng-click="salvar({valor: conteudo})">{{conteudo}}</sdiv>

22.09.2017 / 21:14