0

I’m looking for support on a query using $stateParams in Angularjs. The query is returning undefined.

I have an array of photos in a news feed. Clicking on a photo should take you to a new page of that photo. I'm below attempting to just spit out the photo id in Network google dev tools tab first.

environment: angularjs, javascript parse api using promises/factory/controller/view

Route

.state(�?gallery.item', {
      url: '/item/:galleryid',
      views: {
        menuContent: {
          controller: 'ItemController as vm',
          templateUrl: path + �?/galleryitem/item.html'
        }
      }
    })

Factory - Parse query

function getFirst(Class, term1, term2){

        var defer = $q.defer();
        var ParseString = Parse.Object.extend(Class);
        var query = new Parse.Query(ParseString);
         query.equalTo(term1, term2);
         if(Class==='Gallery') {
            query.equalTo('approve', true);
            query.ascending('createdAt');
         }
        query
        .first()
        .then(function(resp){
          console.log('getFirst', resp);
          if (resp === undefined) {
            defer.reject(resp);
          } else {
            defer.resolve(resp);
          }

        });

    return defer.promise;
  }

return {
getFirst: getFirst
}

ItemController

function ItemController($scope, $state, $rootScope, $stateParams, Factory) {
var vm = this;

getItemDetails();

function getItemDetails() {

vm.Gallery = [];

Factory
.getFirst('Gallery', 'objectId', $stateParams.id)
.then(function(resp) {

vm.GalleryData = response;

 console.log(resp);

 vm.Gallery.push({
            id: resp.id,
        });

      });

Any help would be greatly appreciated.

Thanks

Cameron

1 Answer 1

0

In your routing config you use url: '/item/:galleryid', but in controller you are looking for $stateParams.id which doesn't match routing config

Change to $stateParams.galleryid

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much. The query is coming through now. I still can't seem to show it in my view though.? Can I just do <h2>{{ gallery.id }}</h2> in my item.html?
since Parse is not within aangular context you need to run $scope.$apply() to have angular run digest on view whenever scope is changed externally
Thank you. Its still getting upset. $digest already in progress errors when I run it. From some research, was told to try $scope.$applyAsync();, which makes the errors go away, but data still not binding to view. I'll keep researching. Cheers.
can also try $timeout() ... have to inject $timeout service in controller to use it
Hi Charlie, any chance you could have a look at my other link, which is similar in nature? Understand you are busy and have your own life. If you don't ask, you don't get though. stackoverflow.com/questions/34892102/…

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.