Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing resolve in $routeProvider causes 'Unknown provider ...'
    primarykey
    data
    text
    <p>I am trying to do an asynchronous http request to load some data before my app loads and so I am using a resolve in $routeProvider which is an http request in my MainController. For some reason, I keep getting <strong>Error: [$injector:unpr] Unknown provider: appDataProvider &lt;- appData</strong> where appData is where I do my http request. I am using AngularJS v 1.2.5.</p> <p>Here is the code and two methods that I tried that both give the same error:</p> <p><strong>Method #1</strong></p> <p><strong>MainController.js</strong></p> <pre><code>var MainController = ['$scope','$location','appData', function($scope, $location, appData){ console.log(appData.data); } ]; MainController.loadData = { appData: function($http, $location, MainFactory){ var aid = MainFactory.extractAid($location); return $http({method: 'GET', url: URL_CONST + aid}); } }; </code></pre> <p><strong>app.js</strong></p> <pre><code>var app = angular.module('HAY', ['ngRoute']); app.config(function($routeProvider) { $routeProvider .when('/', { redirectTo: '/pages/alerts' }) .when('/pages/:pageName', { templateUrl: function(params) { return 'views/pages/' + params.pageName + '.html'; }, controller: MainController, resolve: MainController.loadData }) .otherwise({ redirectTo: '/pages/alerts' }); }); </code></pre> <p>I tried changing the name in case it was a conflicting system reserved keyword but with no luck. For some reason, <strong>appData</strong> is never recognized</p> <p><strong>Method #2 I also tried changing it around like so:</strong></p> <p><strong>app.js</strong></p> <pre><code>var app = angular.module('HEY', ['ngRoute']); app.config(function($routeProvider) { $routeProvider .when('/', { redirectTo: '/pages/alerts' }) .when('/pages/:pageName', { templateUrl: function(params) { return 'views/pages/' + params.pageName + '.html'; }, controller: MainController, resolve: { appData: ['$http', '$location','MainFactory', function($http, $location, MainFactory) { var aid = MainFactory.extractAid($location); return $http({method: 'GET', url: URL_CONST + aid}); }] } }) .otherwise({ redirectTo: '/pages/alerts' }); }); </code></pre> <p><strong>MainController.js</strong></p> <pre><code>var MainController = ['$scope','$location','appData', function($scope, $location, appData){ console.log(resolvedData); } ]; </code></pre> <p>However, the result was exactly the same. Does this have something to do with angular 1.2.5 ?</p> <p><strong>Here is a working version from someone else</strong></p> <p><a href="http://mhevery.github.io/angular-phonecat/app/#/phones" rel="nofollow noreferrer">http://mhevery.github.io/angular-phonecat/app/#/phones</a></p> <p>And here is the code:</p> <pre><code>function PhoneListCtrl($scope, phones) { $scope.phones = phones; $scope.orderProp = 'age'; } PhoneListCtrl.resolve = { phones: function(Phone) { return Phone.query(); }, delay: function($q, $defer) { var delay = $q.defer(); $defer(delay.resolve, 1000); return delay.promise; } } angular.module('phonecat', ['phonecatFilters', 'phonecatServices', 'phonecatDirectives']). config(['$routeProvider', function($routeProvider) { $routeProvider. when('/phones', {templateUrl: 'partials/phone-list.html', controller: PhoneListCtrl, resolve: PhoneListCtrl.resolve}). otherwise({redirectTo: '/phones'}); }]); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload