Ng-model with button

时间:2016-04-15 14:53:02

标签: angularjs

Good morning, I make a loop to catch "name " in an array and each "name " has variables with information. I need the ng -model receive the value of the "name " when I click on the "button" to oh yes I can make a loop in this "name".

div class="list-group" ng-repeat="rep in list.report">
 <button type="button" class="list-group-item " ng-model="ctrl.x">
       {{rep.name}}
        </button></div>

2 个答案:

答案 0 :(得分:0)

ng-model won't work with the button use ng-click :

<div class="list-group" ng-repeat="rep in list.report">
   <button type="button" class="list-group-item" ng-click="ctrl.x = rep.name">
   {{rep.name}}
    </button>
</div>

答案 1 :(得分:0)

If I understand your question correctly, is this what you're looking for?

https://jsfiddle.net/3ajtoyfm/

Angular

function Controller() {
  var vm = this;
  vm.rep = null;
  vm.reps = [{
    name: 'Jimmy Page',
    band: 'Led Zeppelin'
  }, {
    name: 'Ozzy Osbourne',
    band: 'Black Sabbath'
  }, {
    name: 'Trent Reznor',
    band: 'NIN'
  }];
  vm.getRep = getRep;

  function getRep(rep) {
    vm.rep = rep;
  }
}

HTML

<button ng-repeat="rep in ctrl.reps" ng-click="ctrl.getRep(rep)">{{rep.name}}</button>
<br>
<div ng-if="ctrl.rep">
  <h4>
     Rep
     </h4> {{ctrl.rep.name}} - {{ctrl.rep.band}}
</div>