1

I have resolve in my route where I return value, which is defined when I console.log it, but in controller it is undefined... I am newbie in Angular and Ionic.

Route

.state('tab.item', {
url: '/collections/item/:itemId',
views: {
  'tab-collections': {
    templateUrl: 'templates/tab-item.html',
    controller: 'ItemCtrl',
    resolve: {
      item: function($localStorage, $stateParams){
        angular.forEach($localStorage.items, function(value, key){
          if (value.id == $stateParams.itemId){
            console.log(value);
            return value;
          }
        });
      },
    }
  }
}

Controller

.controller('ItemCtrl', function($scope, item) {
    $scope.item = item;
    console.log(item);
})

View

<ion-view view-title="{{item.name}}">
    <ion-content>
        {{item}}
        <div>
             <li ng-repeat="image in item.images">
                 <img ng-src="{{image}}" alt="{{item.name}}">
             </li>
        </div>
        <p>{{item.name}}</p>
    </ion-content>
</ion-view>

So why it was working in here?

collection: function($localStorage, $stateParams){
    angular.forEach($localStorage.collection, function(value, key){
       if (value.id == $stateParams.collectionId){
          return value;
       }
    });
 },
1
  • this wouldnt ever work afaik Commented Nov 11, 2015 at 20:53

2 Answers 2

2

Resolve needs you to return a value. You have your return inside of the forEach loop. In order for this to work you will need to return outside of that loop. Perhaps something like:

.state('tab.item', {
url: '/collections/item/:itemId',
views: {
  'tab-collections': {
    templateUrl: 'templates/tab-item.html',
    controller: 'ItemCtrl',
    resolve: {
      item: function($localStorage, $stateParams){
        var result;
        angular.forEach($localStorage.items, function(value, key){
          if (value.id == $stateParams.itemId && !result){
            console.log(value);
            result = value;
          }
        });
        // return outside of the loop
        return result;
      }
    }
  }
}

You might also consider using something other than forEach here since you cannot break out of the loop early. A basic for loop would work just as well.

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

1 Comment

Accept for advice with basic loop
1

you aren't returning a value in your resolve function, returning in a foreach does not do what you think :)

.state('tab.item', {
    url: '/collections/item/:itemId',
    views: {
      'tab-collections': {
        templateUrl: 'templates/tab-item.html',
        controller: 'ItemCtrl',
        resolve: {
          item: function($localStorage, $stateParams){
            var valueToReturn;
            angular.forEach($localStorage.items, function(value, key){
              if (value.id == $stateParams.itemId){
                console.log(value);
                valueToReturn = value;
              }
            });
            return valueToReturn
          },
        }
      }
    }

2 Comments

Weird thing is that it worked in one route before... collection: function($localStorage, $stateParams){ angular.forEach($localStorage.collection, function(value, key){ if (value.id == $stateParams.collectionId){ return value; } }); },
doesnt do anything in this case. in a map function it would be the new items of an array

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.