1

I am getting image in json response form restful api and I have to use that image in html using angularjs.

I dont know how can I use that image using angularJs

If I am trying like that "http:/api/image/id" where id is "userID". But when I write the code in angularjs I didnt get any response.I tried to debug code by using breakpoints and but it didn't go inside the function

JS Code

QAApp.controller('imgCtrl', function ($scope, $http) {
  $scope.image = function (id) {
       var request = $http({
                          method: 'GET', 
                          url: server + 'api/image/' + id,
                        });
            request.success(function(data, status, headers, config) {
              console.log(data);

            });
          }
      }); 

HTML Code

<div class="artst-pic pull-left" ng-controller="imgCtrl">
      <img ng-show = "{{image(q.userID)}}" alt="" class="img-responsive" />K
</div>

Please tell me how can I use this.

3
  • Would help if you could post what your JSON looks like. Commented Sep 1, 2014 at 15:33
  • I am not able to post json response, by using this url localhost:8000/api/image/id, then it will display the image directly not url, so i have to display that image...I didnt get any solution how I can use it.......where /api/image/id this is the api path ... Commented Sep 1, 2014 at 15:41
  • Object {data: "����JFIFHH���ExifMM*…������f�*�c#���B���z�����h���)Ob��5��o�QY��", status: 200, headers: function, config: Object}..... I am getting json response like that Commented Sep 1, 2014 at 15:45

3 Answers 3

2

From what I understand, the API directly sends you the image, not any JSON..

Then just display it like you would display any image (you don't need ajax):

JS

$scope.image = function (id) {
    return server + 'api/image/' + id;
};

HTML

<img ng-src="{{image(q.userID)}}"/>
Sign up to request clarification or add additional context in comments.

2 Comments

Depends if you have access to server side code or not. Anyhow, I encourage you to post a new question with more information on that matter.
I didn't have access to server side, if image is not present the that API path return json response with "No image found", so if this message is come at that time I have to use dummy image....
1

Javascript

QAApp.controller('imgCtrl', function ($scope, $http) {

      $scope.image = function (id) {
             $http({
                              method: 'GET', 
                              url: server + 'api/image/' + id,
                            }).then(function(data){
                             $scope.imageUrl= data; // if you sure what data is you URL 
                       })
              }
          }); 

HTML

<div class="artst-pic pull-left" ng-controller="imgCtrl">
      <img ng-src="{{imageUrl}}" ng-init="image(q.userID)" alt="" class="img-responsive" />K
</div>

1 Comment

please tell me how can I do this, I posted my json response also, please see, how can I solve this problem
0

The Apache Roller source code has an example of using angular.js to bring in an image source: http://svn.apache.org/viewvc/roller/trunk/app/src/main/webapp/WEB-INF/jsps/editor/ThemeEdit.jsp?annotate=1621546 (line 126, with the JavaScript at the bottom of the file).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.