简体   繁体   中英

How to get value from hidden input in ng-repeated div using Angular.js

I have controller which send data to UI and map them using ng-repeat directive.Next thing that i want to do is to bind that data with hidden input form,and send it in another function in controller when click event occur.Any idea how to accomplish this? My HTML looks like this:

<div class="card card-block" ng-repeat="admin in showAdmins">
<h3 class="card-title">{{admin}}</h3>
<input type="hidden" ng-value="{{admin}}" ng-model="username"/>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<button class="btn btn-primary"  ng-click="searchAdmins(username)">SeeProfile</button>
</div>

And controller:

$scope.searchAdmins = function(username){

    //To do
};

github.users('admins.php').then(function(users){

    $scope.showAdmins = users;
    console.log($scope.showAdmins);

});

I think you want to do this:

<div class="card card-block" ng-repeat="admin in showAdmins">
    <h3 class="card-title">{{admin}}</h3>
    <input type="hidden" ng-model="admin"/>
    <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
    <button class="btn btn-primary"  ng-click="searchAdmins(admin)">SeeProfile</button>
    </div>

http://plnkr.co/edit/IzJkvLJoTHLtVzobWPpZ?p=preview

var app = angular.module('app', []);

app.controller('testctrl',function($scope) {

var users = [

{
  user : 'User1'
}, {
  user : 'User2'
}, {
  user : 'User3'
}

];

$scope.showAdmins = users;

$scope.searchAdmins = function(user){

alert(user); }

});

Html

<div  ng-repeat="admin in showAdmins">
<h3 >{{admin.user}}</h3>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<button class="btn btn-primary"  ng-click="searchAdmins(admin.user)">SeeProfile</button>


Thank you, Chaitanya

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM