从ng-click事件获取对控件的引用

时间:2016-02-02 15:09:16

标签: angularjs

我有一个动态填充可点击图像的表格行。单击图像时,我需要将其唯一ID传递给控制器​​。我尝试了很多不同的方法,但到目前为止都没有成功。我尝试了什么:

<td ng-repeat="Type in Types">
    <img ng-src="../../Images/{{Type.Path}}" ng-title="{{Type.TypeId}}" ng-model="n_type" ng-click="Process()" />
</td>

 $scope.Process = function () {
    console.log($scope.n_type);
    $scope.$parent.n_type = $scope.n_type;
 }

<td ng-repeat="Type in Types">
    <div data-ng-init="imgId='{{Type.TypeId}}';">
        <img ng-src="../../Images/{{Type.Path}}" data-ng-attr-id="imgId" ng-title="{{Type.TypeId}}" ng-model="n_type" ng-click="Process(imgId)" />
    </div>   
</td>

 $scope.Process = function () {
    console.log($scope.imgId);
    $scope.$parent.n_type = $scope.imgId;
 }

但在这两种情况下,结果都是“未定义”

1 个答案:

答案 0 :(得分:1)

将ID作为参数传递给您的Process函数:

<td ng-repeat="Type in Types">
    <img ng-src="../../Images/{{Type.Path}}" ng-click="Process(Type.imgId)" />  
</td>

然后

$scope.Process = function (imgId) {
    $scope.$parent.n_type = imgId;
 }