0

I have a simple AngularJS application, i have the page with albums at the Albums.html page, after click to album i redirect to AlbumPhotos.html and send $http.post request to server and get all photos of album from server:

//Empty Photo Array
$scope.AllPhotosOfAlbumArray = [];
// Get All Photos Of Album
$scope.getAlbumPhotos = function (albumId) {
    console.log(albumId);
    logicOfMyApp.getAllPhotosOfALmubFromSrverFunc(albumId).then(function (photos) {
        $scope.AllPhotosOfAlbumArray = photos;
        console.log($scope.AllPhotosOfAlbumArray);
    });
};

getAllPhotosOfALmubFromSrverFunc() method code:

getAllPhotosOfALmubFromSrverFunc: function(albumId)
    {
        var photosPromise = $q.defer();
        var req = {
            method: 'POST',
            url: '/api/getPhotos',
            data: albumId
        };
        $http(req).success(function(res) {
           return photosPromise.resolve(res)
        }).error(function(error){
            return photosPromise.reject(error);
        });
        
        return photosPromise.promise;
     }

I get data from server: enter image description here

But when i trying display data from $scope.AllPhotosOfAlbumArray use ng-repeat directive at the page:

<!--Display all Photos Of Album-->
<div ng-repeat="photo in AllPhotosOfAlbumArray">
    {{photo}}
</div>

it display nothing.

Maybe somebody knows how i can resolve it ?

Thanks for your answers!

10
  • Is there any error in console? Commented Aug 21, 2016 at 8:11
  • @CeylanB. No I haven't errors. Commented Aug 21, 2016 at 8:13
  • Try add some value to photo array and check if they will show Commented Aug 21, 2016 at 8:15
  • @Geding I added static data like this [1,2,3,4] and it displays. Commented Aug 21, 2016 at 8:16
  • Can you check if that ng-repeat is working with data from server adding inside some text (and check if it display)? Commented Aug 21, 2016 at 8:18

1 Answer 1

1

Something i noticed in your code is return photosPromise.resolve(res) and return photosPromise.reject(error); you don't need to use return on those lines because that is not the way to use $q in angularjs. here is a snippet from $q documentation.

function asyncGreet(name) {
  var deferred = $q.defer();

  setTimeout(function() {
    deferred.notify('About to greet ' + name + '.');

    if (okToGreet(name)) {
      deferred.resolve('Hello, ' + name + '!');
    } else {
      deferred.reject('Greeting ' + name + ' is not allowed.');
    }
  }, 1000);

  return deferred.promise;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I change my method like you, it return photos, but it still doesn't display, I use another way to resolve it, I using a data-ng-init directive, and get data after load the page.

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.