Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, December 24, 2015

AngularJS Error: $injector:unpr Unknown Provider

Can someone point me in the right direction? I have done my best to follow the documentation, and looking through SO most of the related issues are much more in depth, and more difficult for me to understand. This is my first time creating a service.

Answer by mpm for AngularJS Error: $injector:unpr Unknown Provider


app.factory('getSettings', ['$http','$q' /*here!!!*/,function($http, $q) {  

you need to declare ALL your dependencies OR none and you forgot to declare $q .

edit:

controller.js : login, dont return ""

Answer by Kunal Shewale for AngularJS Error: $injector:unpr Unknown Provider


Your angular module needs to be initialized properly. The global object app needs to be defined and initialized correctly to inject the service.

Please see below sample code for reference:

app.js

var app = angular.module('SampleApp',['ngRoute']); //You can inject the dependencies within the square bracket        app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {    $routeProvider      .when('/', {        templateUrl:"partials/login.html",        controller:"login"      });      $locationProvider      .html5Mode(true);  }]);    app.factory('getSettings', ['$http', '$q', function($http, $q) {    return {      //Code edited to create a function as when you require service it returns object by default so you can't return function directly. That's what understand...      getSetting: function (type) {         var q = $q.defer();        $http.get('models/settings.json').success(function (data) {          q.resolve(function() {            var settings = jQuery.parseJSON(data);            return settings[type];          });        });        return q.promise;      }    }  }]);    app.controller("globalControl", ['$scope','getSettings', function ($scope,getSettings) {    //Modified the function call for updated service    var loadSettings = getSettings.getSetting('global');    loadSettings.then(function(val) {      $scope.settings = val;    });  }]);  

Sample HTML code should be like this:

                  Sample Application                      
Your UI elements go here

Please note that the controller is not binding to an HTML tag but to the body tag. Also, please try to include your custom scripts at end of the HTML page as this is a standard practice to follow for performance reasons.

I hope this will solve your basic injection issue.

Answer by Franz1986 for AngularJS Error: $injector:unpr Unknown Provider


@ user2310334 I just tried this, a VERY basic example:

HTML file

                                    

  11 comments:


  1. Very informative content that guided me to know the details about the training offered in different technology. Angular JS is a structural framework for dynamic web application and angular's data binding eliminates much of the code that you would otherwise write it.
    Angularjs Training in Chennai | angularjs course in chennai

    ReplyDelete
  2. • I love all the posts, I really enjoyed, I would like more information about this, because it is very nice., Thanks for sharing.
    tib co training in chennai

    ReplyDelete
  3. Finish the Selenium Training in Chennai from Infycle Technologies, the best software training institute in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Java, Hadoop, Big Data, Android, and iOS Development, Oracle, etc with 100% hands-on practical training. Dial 7502633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.

    ReplyDelete

Popular Posts

Powered by Blogger.