How do I use dynamic information coming by ng-repeat {{}} and use it as the argument of an ng-click (button) function?

Asked

Viewed 105 times

2

I have a function ng-click:

ng-click = "adicionar(0);"

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

There is a way to pass on information according to the repeat in which it is contained?

Way I’m trying, unsuccessfully:

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

Follows code Angularjs:

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 in which it is contained:

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

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

  • Your index has to be static? if you can’t pass it with add($index)

2 answers

3


Do not need {{}} just remove this.

example:

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

or

<button ng-click = "adicionar(mercadoria.mercadoria)"> + </button>
  • OMG as I had not thought about it? THANK YOU VERY MUCH Ccastro!

2

Ccastro’s answer is correct - however I think it is worth an explanation of which cases need the markers, and which not:

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

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

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

Browser other questions tagged

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