0

I have following lines of code in my AngularJS project.

<div ng-show="update">
<table border="1">
    <tr ng-repeat="x in names">
        <td>{{ x.uname}}</td>
        <td>{{ x.upass}}</td>
        <td><button ng-model="index" ng-click="show()" 
                     ng-value="{{x.index}}">Edit</button></td>
    </tr>
</table>    

When Particular button is clicked I want to retrieve that button label. For that I have written code in Show function:

var v = $scope.index;
alert(v);

But alert box is displaying "Undefined" when i click on button. Please suggest me where I am wrong?? Thanks in advance

4
  • Well... There really isn't any reason to bind the button with the ngModel directive. What are you trying to achieve exactly? Commented Aug 11, 2015 at 11:23
  • 2
    ng-model does not work on button tag Commented Aug 11, 2015 at 11:23
  • What do you mean by button label? Commented Aug 11, 2015 at 11:31
  • I mean I want to pass index to function from button when button is clicked Commented Aug 11, 2015 at 11:38

4 Answers 4

1

What about providing your value as an argument to the onclick function ?

<div ng-show="update">
<table border="1">
    <tr ng-repeat="x in names">
        <td>{{ x.uname}}</td>
        <td>{{ x.upass}}</td>
        <td><button ng-click="show(x.index)">Edit</button></td>
    </tr>
</table> 

and then

function show(id){

    alert(id);
}

This should works. Also ng-model does not work on button.

Sign up to request clarification or add additional context in comments.

2 Comments

why you not use id param inside function?
Sorry my mistake, I did it a bit quickly. it is fixed :D. Thx @Ankit21ks for suggesting the edit.
1

under ng-click = show(item) - this gives the particular item object.

    function show(obj){ console.log(obj)}

this function give the particular clicked obj from view.

ng-model doesn't work on button.

Comments

0

You can pass the index of the repeat iteration to the show method like this:

<button ng-click="show($index)" ...

and use it like this:

function show(i){
  var v = names[i];
  alert(v);
}

You can see it in action here: PLNKR DEMO

NOTE: if you are passing the index to the backend service the index may not match up with the actual array if you are using a filter in your view to alter/reduce the ng-repeat expression

Another option as pointed out by @Grundy is to just pass the object back, which saves you the step of having to access it from the array... (note: this is the suggested best practice)

2 Comments

if you pass x directly, you not need names[i]
@Grundy you are correct, I had it in my plnkr, but didn't save the right URL.... but I updated my answer to show the passing of an object.
0

I am assuming that you want the button label that you have clicked on and the index of it in the list provided. Here is what you can do.

<div ng-repeat="buttonId in testdata2">
  <button ng-bind="buttonId" ng-click="clickedMe(buttonId, $index)"></button>
</div>




$scope.clickedMe = function(buttonId, index) {
 console.log(buttonId + " - " + index);
};

Have your files in local and run them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.