2

I need to fetch data from my database, updated a div and onclick update another div.
I managed to fetch the data but I fail to load additional by onCLick.

My Code:

var app = angular.module('mainApp', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});

app.controller('mainController', function($scope, $http) {

$scope.fetcheddata = [];
$scope.loading = false;

$scope.init = function() {
    $scope.loading = true;
    $http.get('/api/content').
    success(function(data, status, headers, config) {
        $scope.fetcheddata = data;
            $scope.loading = false;

    });
}
$scope.getDesc = function() {
    $scope.loading = true;
    $http.get('/api/content/get/' + item.id).
    success(function(data, status, headers, config) {
        $scope.fetcheddata = data;
            $scope.loading = false;

    });
}

$scope.init();

});

View Code:

 <div class="col-xs-2 type-image" tr ng-repeat='item in fetcheddata'>
     <a href="#" class="thumbnail click-productgroup" data-group="show-<% item.group %>">
       <img src="<% item.image %>" class="img-responsive">
     </a>
  </div>  

The -tag should fetch the item.description and update another div. How can I do that?
I tried with an getDesc scope. Is this right? But how do I updated the div?

EDIT 1:

                <div class="row">
                <div class="col-md-7" id="productGroups">
                        <div class="row type-row">

                            <div class="col-xs-2 type-image" ng-repeat='item in fetchedData'>
                                <a href="#" class="thumbnail" ng-click='getDesc(<% item.id %>)'>
                                    <img src="<% item.image %>" class="img-responsive">
                                </a>
                            </div>

                        </div>
                </div>
                <div class="col-md-5 type-description">
                    <% fetchedDesc | json %>
                </div>
            </div>  

and in my controller:

$scope.getDesc = function() {
    $scope.loading = true;
    $http.get('/api/content/get/' + item.id).
    success(function(data, status, headers, config) {
        $scope.fetchedDesc = data.description;
            $scope.loading = false;

    });
}  

Still the page jumps on click to the top and no content in the div (although ng-binding appears in the html).

5
  • Do you get any error? Commented Sep 11, 2015 at 6:17
  • No, because I don't know how to fetch it! Commented Sep 11, 2015 at 6:19
  • you just have to place {{ item.description }} where you want to show it isnt it? Commented Sep 11, 2015 at 6:21
  • use, but I those are several items which are loaded in the ng-repeat. I need one description on click for a chosen item after the load. Commented Sep 11, 2015 at 6:24
  • Oh I see you want to show description on click of anchor tag? Commented Sep 11, 2015 at 6:26

2 Answers 2

2

Firstly, the page is jumping because the href of your anchor tag is #, remove that to stop it from happening :).

Secondly, I think the best way of achieving what you're after would be to ensure that each item fetched from the server in the $http.get('/api/content') call has the description as a property, rather than fetching it seperately, and then do something like this:

$http.get('/api/content').
    success(function(data, status, headers, config) {
        $scope.items = data;
    });


$scope.selectItem = function(item) {
    $scope.selectedItem = item;
}

And in the view:

<div class="col-xs-2 type-image" tr ng-repeat='item in items'>
    <a href="" class="thumbnail click-productgroup" ng-click="selectItem(item)">
        <img src="{{ item.image }}" class="img-responsive">
    </a>
</div>
<div>
    {{ selectedItem.description }}
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

this is it... I also accidentally added the <% tags to selectItem() - now it works! Thank you!
1

I notice that is your getDesc you don't pass item.id?

$scope.getDesc = function(item) {
 $http.get('/api/content/get/' + item.id)
   .then(function(data) {
      console.log(data)
   })
}

This should come from ng-click of the div tag?

 <div class="col-xs-2 type-image" tr ng-repeat='item in fetcheddata'>
     <a href="#" class="thumbnail click-productgroup" data-group="show-<% item.group %>" ng-click="getDesc(item)">
       <img src="<% item.image %>" class="img-responsive">
     </a>
  </div> 

1 Comment

I did that know. Still the page jumps and I the description div has ng-binding but now content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.