从ng-repeat获取id

时间:2015-06-30 12:13:47

标签: javascript angularjs

在此代码中,如何获取函数user.Id中传递的ng-click="UpdateUser()"的值?

  



> 
>     <tr ng-repeat="user in users | filter: search ">
>     <td>{{ user.Id }}</td>
>     <td><button type="button" ng-click="UpdateUser()" >Update</button></td>
> <tr>
> 
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

更新ng-click的{​​{1}},如下所示:

button

<强>代码

查看:

ng-click="UpdateUser(user.Id)"
//                   ^^^^^^^

控制器:

<button ng-click="UpdateUser(user.Id)">Update</button>
//                           ^^^^^^^

答案 1 :(得分:1)

Try to pass the user.id to updateUser().

Check if your codes are like the codes below.

Here's the JsFiddle link.

HTML

<div ng-controller="YourCtrl">
    <table style="width:100%">
        <tr ng-repeat="user in users| filter: search">
            <td>{{ user.Id}}</td>
            <td><button type="button" ng-click="updateUser(user.Id)" >Update</button></td>
        <tr>
    </table>
    <br />
    Selected UserId: {{selectedUserId}}
</div>

Your Controller

'use strict';
var app = angular.module('myApp', []);
app.controller('YourCtrl', ['$scope', function($scope) {

        $scope.users = [
            {
                "Id": 2609,
                "FullName": "Hasan Ibn Alhaitham",
                "Email": "abas@farnas.com",
                "Mobile": "5642597952",
                "Birthdate": "1966-06-‌​15T21:00:00.000Z",
                "Gender": "Male",
                "AcademicLevel": "Master"
            },
            {
                "Id": 2615,
                "FullName": "Welcome page",
                "Email": "welcome@page.com",
                "Mobile": "3243654645",
                "Birthdate": "2015-06-09T2‌​1:00:00.000Z",
                "Gender": "Male",
                "AcademicLevel": "High School"
            }
        ];

        $scope.updateUser = function(userId) {
            console.log(userId);
            $scope.selectedUserId = userId;
        };

        console.log($scope.users);

    }]);

I added selectedUserId variable to check if it reflects to the view.

Note: Please observe camelCase naming convention.

Hope it helps.