Mr_Desmond Mr_Desmond - 5 months ago 7
AngularJS Question

Angularjs Get value of Repeated button

here i get a simpel html tabel for every x in names. x represent an Object with a few attributes.so one row of this tabel is filled with: Name,id and so on. i also get one button for every row(Object). But how can i check wich button is pressed? How can i get a value from this button?
For Exampel there is one row with name: Michael, id: 1 and one button. If i press the button i want a var with the id:1 in my app.controller.

HTML code

<ul >
<li ng-repeat="x in names | filter:name">
<div id="kartei">{{ x.artikelName }}{{ x.artikelBeschreibung }}{{ x.artikelPreis }}{{ x.angebotNummer }}{{ x.kundenMail }}</div>

<div id="Kaufen" ng-app="myApp" ng-controller="angebotKaufen">
<button ng-model="kaufbutton" type="button" ng-click="submit()" ng-value="{{ x.angebotNummer }}" >kaufe {{ x.angebotNummer }}</button>
</div>


</div>
</li>
</ul>


JavaScript

app.controller("angebotKaufen", function($scope, $location){
$scope.submit = function(){
console.log($scope.kaufbutton);
// Kaufen klicken
// id infos von der datenbank bekkomen
// kaufen weiterleiten



};

Answer

you should just pass kaufbutton through the function.

Html

<ul >
  <li ng-repeat="x in names | filter:name">
  <div id="kartei">{{ x.artikelName }}{{ x.artikelBeschreibung  }}{{ x.artikelPreis  }}{{ x.angebotNummer  }}{{ x.kundenMail  }}</div>

<div id="Kaufen" ng-app="myApp" ng-controller="angebotKaufen">
<button ng-model="kaufbutton" type="button" ng-click="submit($event, kaufbutton)" ng-value="{{ x.angebotNummer }}" >kaufe {{ x.angebotNummer }}</button>
</div>


</div>
</li>
</ul>

Code

app.controller("angebotKaufen", function($scope, $location){
    $scope.submit = function($event, kaufbutton){
        $event.preventDefault();//just in case you need this
        console.log(kaufbutton);
        // Kaufen klicken
        // id infos von der datenbank bekkomen
        // kaufen weiterleiten      
    }; 


});
Comments