1

I am a bit confused. I am trying to get my resolve data in my controller. I read about these (and more) solutions, but can not get it working. It is all about the "spages".

http://jsfiddle.net/Avb4U/1/

http://www.jvandemo.com/how-to-resolve-angularjs-resources-with-ui-router/#

Angularjs resolve with controller as string

I hope this is not a duplicate question, because nothing did work for me from these solutions.

This is (part of all states) my state:

.state('root.search', {
    url: '/search/:searchterm',
    onEnter: ['$rootScope', function($rootScope) {
        $rootScope.bodyclass = 'search';
    }],

    resolve : {
        spages: ['$stateParams', 'SearchPages', function($stateParams, SearchPages) {
            return SearchPages.get({'searchterm': $stateParams.searchterm});
        }]
},
    controller: 'searchCtrl',
    views: {
        '@': {
              templateUrl: templateUrlFunction('search')
             }
    }

})

This is part of my controller:

app.controller('searchCtrl', ['$scope', 'spages', function($scope, spages) {
    // spages should be already resolved and injected here
}

And this is my factory for the searchpages:

app.factory('SearchPages', ['$resource', function($resource) {
    return $resource(null, {},
            {
                get: {
                    method: 'GET',
                    url: '/json/search/get/searchterm/:searchterm',
                    params: {searchterm: '@searchterm'}
                }
            });
}]);

Asfar as i do understand, spages should be resolved and injected in the controller now. But it is not.

The error i get is:

Error: [$injector:unpr] Unknown provider: spagesProvider <- spages

What do i do wrong? I am still learning...


Ok, what i did not realize is that i also have a controller in my template.

And i read: "You only need to specify controller once, in app.js. The second one, in the html code, is instantiated by the ngController directive, which does not know about `resolve', so it throws an exception."

Community
  • 1
  • 1
ProX
  • 297
  • 1
  • 3
  • 16
  • Check, but that should not be a problem. The controller needs the 'searchpages' string from the resolve. However, better naming would avoid confusion i agree. I did edit my code so it is less confusing. – ProX Aug 14 '14 at 14:46
  • Did you find a solution after all this time? – Lucio Mar 28 '15 at 21:06
  • Sorry Lucio, I do not remembr. It has been a while with a different company... I think i did an ajax call on the submit button and then: `code`$scope.searchpages.pages = result; – ProX Mar 31 '15 at 12:01

1 Answers1

0

Change your controller dependency from searchpages to SearchPages.

app.controller('searchCtrl', ['$scope', 'SearchPages', function($scope, searchpages) {
    // searchpages should be already resolved and injected here
}
Darshan P
  • 2,241
  • 18
  • 16
  • 1
    I think my name giving was confusing. Sorry about that. I edited my code. As far as i understand the docs, "spages" should be available in the controller after the resolve. – ProX Aug 14 '14 at 14:51
  • Yes that should work. i didn't see that when i posted my answer. – Darshan P Aug 14 '14 at 14:51