Ng-repeat and ng-click sending always the first element of the array


#1

Hi, I don’t know if it his the right place to ask, so forgive me if I put in the wrong section.

Well I’m trying to call a function using parameters which come from an array, using ng-repeat.

<label class="item">
          <span class="input-label">Available tags</span>
            <div class="item-text-wrap">
                <button type="button" ng-repeat="tag in allTags" ng-click="selectTag(tag);" class="button button-icon">#{{tag}} </button>
            </div>
        </label>

But for some reason, every time I click in any tag (let’s say: ‘apple’, ‘orange’, ‘banana’, ‘pear’), instead of selecting the tag I clicked, it always select the first one in the list. So, if I click on ‘banana’, the method will always receive ‘apple’ instead.

here is my method:

$scope.selectTag = function(tag) {
      console.log('Selected tag: ' + tag);
  };

This code is inside a , which is in a modal.

If I change to send the $index instead of the tag, the method always receive 0.

Is there something wrong with my code?

Thanks in advance.


#2

Sorry, just found it

The problem is that the button is inside a element.

Just changed it to span and it worked :smile:

Thanks everyone :smiley: