1

I have the following code in my application (stripped out otiose code so there might be typos)

Basically I need to be sure that $scope.takeovers has data before any other code is executed.

When monitoring network activity in the browser I can see that /api/pagedata is being called and successfully outputs the data I want in a $scope

However $scope.takeovers is undefined always.

Currently I have this which is not working.

app.js

var app = angular.module('app', ['ui.router']);

app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/');

    $stateProvider
    .state('index', {
        url : '/',
        resolve: {
            takeovers: function(pageSetup) {
                return pageSetup.getData().$promise;
            }
        },
        controller: 'newFormController',
        templateUrl: '/templates/form.html'
    })
});

app.controller('newFormController', ['$scope', 'takeovers', function($scope, takeovers) {

    $scope.takeovers = takeovers.data; //Is empty when controller is initiated...

    /* continue with code that rely on $scope.takeovers */


}]);

app.factory('pageSetup', ['$http', function($http) {
  var res = {
    getData: function() {
      var promise = $http({ 
        method: 'GET', 
        url: '/api/pagedata' 
      });
      promise.success(function(data, status, headers, conf) {
        return data;
      });
      return promise;
    }
  }
  return res;
}]);

/api/pagedata result

["2015-11-01","2015-11-02","2015-11-03","2015-11-08","2015-11-09","2015-11-10","2015-11-11","2015-11-15","2015-11-16","2015-11-29","2015-11-30"]

What am I missing?

2
  • The resolve is making sure all data is resolved before instantiating the controller. Looking at your code my guess would be there is no field data in takeover as takeover itself is your data. $scope.takeovers = takeovers; should suffice. Commented Dec 8, 2015 at 15:47
  • where does $promise comes from? there is not such property Commented Dec 8, 2015 at 15:52

1 Answer 1

4

I think the final part of return pageSetup.getData().$promise; is your problem. getData already returns a promise so all you need to do is to return it directly.

return pageSetup.getData();

You can also actually resolve the promise directly in your resolve function and return the result of it. I don't think that actually changes anything in the simple case, but it lets you manipulate the result or grab a specific part of it before sending it to the controllers. So something along the lines of:

resolve: {
    takeovers: function(pageSetup) {
        pageSetup.getData().then(function(response) {
            return response.data;
        });
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank's for your input. I have tried that already but it doesn't work. When I console.log takeovers it outputs some kind of objec with alot of strange properties. Before expanding it it says Object {defaults: Object, version: "10.0.2"}
Even with the second version? I think a resolve is supposed to resolve promises on its own, but if you do it explicitly then I don't see how it could fail.
Unless your promise.success confuses it, that part of your code doesn't really do anything.

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.