如何在angularjs中选择图像时,在控制器中获取图像ID

时间:2016-07-22 07:08:36

标签: html angularjs

如何在选择图像时在控制器中获取图像ID

我的html文件

<ul class="thumbnails">
   <li class="span3" ng-repeat="reward in rewardData.results">
     <a class="thumbnail" href="#">
       <img src="data:image/png;base64,{{reward.image}}" />
     </a>
   </li>
</ul>
<div style="display:block;width:60%;">
  <a href="#invite" class="btn btn-theme invitefr" ng-click="selectPrizes()"> Save Challenge and Continue</a>
</div>

它会显示多个图像,我想在我的控制器中有一个图像ID而有些人选择任何一个图像,任何人都可以知道如何实现它吗?

我的控制员:

app.controller("PrizesController", ["$location", "$scope","authenticationSvc","$http", function ($location, $scope,authenticationSvc, $http) {
    console.log("inside prize controller");

    var token = authenticationSvc.getUserInfo();
    var config = {
        headers: {
            'h5cAuthToken': token.accessToken,
            'Accept': 'application/json;odata=verbose'
        }
    };

     $http.get("http://IPandPortnumber/ccp-services/challengereward/allRewards", config)
        .then(function (response) {
            $scope.rewardData = response.data;

        });

    $scope.selectPrizes = function () {
      // some block of codes       
       $location.path("/invite");
    }
}]);

2 个答案:

答案 0 :(得分:1)

将图片ID传递给getImgId(reward.id)函数

 <ul class="thumbnails">
   <li class="span3" ng-repeat="reward in rewardData.results">
    <a class="thumbnail" href="#">
   <img src="data:image/png;base64,{{reward.image}}" ng-click="getImgId(reward.id)"/>
  </a>
 </li>
 </ul>
<div style="display:block;width:60%;">
   <a href="#invite" class="btn btn-theme invitefr" ng-  click="selectPrizes()"> Save Challenge and Continue</a>
 </div>

  //inside controller
  $scope.getImgId= function (id) {
  console.log(id);
}

答案 1 :(得分:0)

在你的控制器中试试这个

  $scope.imgId='';
  $scope.getImgId= function (id) {
   console.log(id);
   $scope.imgId=id;
 }


  $scope.selectPrizes = function () {
  // You will get selected img id here  
   console.log($scope.imgId);
   $location.path("/invite");
}