3

I am trying to fetch a drop downlist before the page load using angular $http. I tried few combinations but it keep on giving the same error:

Error: [$injector:unpr] Unknown provider: officeListProvider <- officeList <- myController http://errors.angularjs.org/1.4.3/$injector/unpr?p0=officeListProvider%20%3C-%20officeList%20%3C-%20myController

I am few weeks old in angular so please pardon in case of any silly mistakes.

var myApp = angular.module('myApp',['ngRoute']);

myApp.config(['$routeProvider',function ($routeProvider) {  
    $routeProvider.when('../../home/goEeUpdateAngular.obj', {
        templateUrl: '/employee_update_angular.jsp', 
        controller: 'myController',
        resolve: {
            officeList: function(officeListFactory) {
                return officeListFactory.getOfficeList();
            }
        }
    });   
}]);

myApp.factory('officeListFactory', function($http, $window) {
    $window.alert("Hi");
    var factoryResult = {
        getOfficeList: function() {
            var promise = $http({
                method: 'GET', 
                url: '../../home/goOfficesList.obj' 
            }).success(function(data, status, headers, config) {
                console.log (data);
                return data;
            });

            return promise;
        }
    }; 

    console.log (factoryResult.getOfficeList());
    return factoryResult;
});

myApp.controller('myController',function ($scope,officeList) {
    $scope.officeListFactory = officeListFactory.data;
});

2 Answers 2

1

The error says "officeListProvider" is not present or not visible, you need to add it as dependency.

Please try the below change:

var ctrl = angular.module('myApp.controllers', []);

to

var ctrl = angular.module('myApp.controllers', ['myApp.services']);

and also please use the same service name it is either srvOfficeList or officeList, and also check your service factory, it is not right - example:AngularJS : factory $http service

Hope it will fix the issue.

Please try to create a CodePen (or similar tool) while posting the question, so that the Answer can tried/fixed in there and shared back with you.

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

4 Comments

Thanks Surya, based on your suggestions, I updated the code. Now the console.log (data) [see above], prints the 1850 records as expected but how should I hook it up to $routeProvider.resolve() method. I want to load the drop down before the page initialize.
load the drop down before the page initialises? what do you mean? the data is in client-side javascript and you need to import that into your html using AngularJS Controller - how to load data from resolve to controller, check this: odetocode.com/blogs/scott/archive/2014/05/20/…
I want to get data from database before page load in angular. Please see the URL below. tech-blog.maddyzone.com/javascript/get-data-page-load-angular.
I used the URL u mentioned , they use like - resolve: { message:...................................... and than inject the same in controller app.controller("newsController", function (message). See here name "message" is same in controller and routerProvider function but when I did the same [see the code i just updated] the injector service not able to recognize the provider.This is the error I get : Error: [$injector:unpr] Unknown provider: officeListProvider <- officeList <- myController
0

In controller you should call only officeList. Here is the working JSFIDDLE. I too sample webapi instead of your url

var myApp = angular.module('myApp',['ngRoute']);

myApp.config(['$routeProvider',function ($routeProvider) {  
    $routeProvider.when('../../home/goEeUpdateAngular.obj', {
        templateUrl: '/employee_update_angular.jsp', 
        controller: 'myController',
        resolve: {
            officeList: function(officeListFactory) {
                return officeListFactory.getOfficeList();
            }
        }
    });   
}]);

myApp.factory('officeListFactory', function($http, $window) {
    $window.alert("Hi");
    var factoryResult = {
        getOfficeList: function() {
            var promise = $http({
                method: 'GET', 
                url: '../../home/goOfficesList.obj' 
            }).success(function(data, status, headers, config) {
                console.log (data);
                return data;
            });

            return promise;
        }
    }; 

    console.log (factoryResult.getOfficeList());
    return factoryResult;
});

myApp.controller('myController',function ($scope,officeList) {
    $scope.officeListFactory = officeList.data; //changes are made here
});

1 Comment

Happy to help always.

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.