ashwani bakshi Ответов: 1

Невозможно передать объект ng-repeat в функцию с помощью ng-click?


  <table  class="table">
            <thead>
                <tr><th>pcat</th><th>psub</th><th>ocas</th><th>metal</th><th>Edit</th></tr>
            </thead>
            <tbody>
<tr ng-repeat="a1 in co">
<td>{{a1.pcat}}</td><td>{{a1.psub}}</td><td>{{a1.ocas}}</td><td>{{a1.metal}}</td><td><input type="hidden"/><input type="button" id="b1" value="Edit" ng-click=edit(a1) /></td></tr>
            </tbody>
        </table> 


$scope.edit = function (a1) {
        alert(a1.data);
        $scope.da = a1.data;
    };


Что я уже пробовал:

я использовал ng-init, но это не сработало

1 Ответов

Рейтинг:
12

Karthik_Mahalingam

обратитесь к этой проверке исправлений,

 alert(a1.data); 
// there is no property with data, you will have to use any one of the properties (pcat,psub,ocas,metal) in the object  

alert(a1.pcat);
    $scope.da = a1.pcat;


<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope) {
            var data = [
                { pcat: 'pcat-1', psub: 'psub-1', ocas: 'ocas-1', metal: 'metal-1' },
                { pcat: 'pcat-2', psub: 'psub-2', ocas: 'ocas-3', metal: 'metal-4' },
                { pcat: 'pcat-3', psub: 'psub-2', ocas: 'ocas-3', metal: 'metal-4   ' },
            ];
            $scope.co = data;
            $scope.edit = function (a1) {
                alert(a1.pcat); 
                $scope.da = a1.pcat;
            };
            
        });
    </script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
     
    <table class="table">
        <thead>
            <tr><th>pcat</th><th>psub</th><th>ocas</th><th>metal</th><th>Edit</th></tr>
        </thead>
        <tbody>
            <tr ng-repeat="a1 in co">
                <td>{{a1.pcat}}</td>
                <td>{{a1.psub}}</td>
                <td>{{a1.ocas}}</td>
                <td>{{a1.metal}}</td>
                <td><input type="hidden" /><input type="button" id="b1" value="Edit" ng-click=edit(a1) /></td>
            </tr>
        </tbody>
    </table>
</body>
</html>

Демонстрация: Плунжер[^]