Em Ae
Em Ae

Reputation: 8734

AngularJS passing variable from html to controller on href

I know basic angular and i am working on a project in which i hve this code

<div>
    <a href="#!/views/products?productId=98" target="_blank">
        <img src="images/x-{{product.productId}}.png"/>
    </a>
</div>

Right now, I have productId as a hardcoded value. What i want that this productId is actually product.productId and in controller, I can use this productId when it is clicked to fetch further details.

Here is my ProductController

.controller('ProductCtrl',function($scope, $http) {
      $http.get('http://localhost:8080/api/products').

        success(function(data) {
              $scope.product = data;
        });
})

I want http://localhost:8080/api/products this to pass a queryParameter as http://localhost:8080/api/products?id=XX where id is product.productId from the html i put above.

How can i acehive this.

Upvotes: 0

Views: 2447

Answers (1)

bresleveloper
bresleveloper

Reputation: 6066

<div>
    <a href="#" ng-click="getMoreDetails(product.productId)" target="_blank">
        <img src="images/x-{{product.productId}}.png"/>
    </a>
</div>

.controller('ProductCtrl',function($scope, $http) {
   $scope.getMoreDetails = function(id){
      $http.get('http://localhost:8080/api/products' + id).

        success(function(data) {
              $scope.product = data;
        });
})

and maybe just use a dialog or lightbox

Upvotes: 1

Related Questions