Chris Jeong
Chris Jeong

Reputation: 185

angularjs resolve is undefined

so in my state, i have

    angular.module('app', ['ui.router', 'chart.js'])
        .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
            $urlRouterProvider.otherwise('/');
            $stateProvider
                .state('home', {
                    url: '/home',
                    component: 'home',
                    resolve: {
                        data: ['$http', function ($http) {
                            return $http.get('some api call')
                                .then(function (response) {
                                    console.log("this is the response", response);
                                    return response;
                                });
                        }]
                    }
                });
        }]);

then i get the proper response back. but when i check my resolve in here,

angular.module('app')
    .component('home', {
        templateUrl: 'Content/app/components/home.html',
        bindings: {
            resolve: '<'
        },
        controller: [
            function () {
                var vm = this;
                vm.$onInit = function () {
                    console.log("this is the resolve", vm)

                }

            }]
    });

i see that my resolve is undefined. Am i doing something wrong?

Upvotes: 1

Views: 454

Answers (1)

Tristan Hessell
Tristan Hessell

Reputation: 950

$stateProvider will bind what you specify inside the resolve object to your component, rather than binding the whole resolve object itself.

angular.module('app')
  .component('home', {
    templateUrl: 'Content/app/components/home.html',
    bindings: {
        data: '<'
    },
    controller: [
      function () {
        var vm = this;
        vm.$onInit = function () {
          console.log("this is the resolve", vm)
        }
      }]
  });

Documentation link: https://ui-router.github.io/ng1/docs/latest/interfaces/state.statedeclaration.html#as-an-object

Upvotes: 1

Related Questions