Greatym.com Greatym.com - 5 months ago 32
HTML Question

Angular js returning error Unknown provider:

I created a factory dtoResource.js to work with my data as per different form.

I am trying to inject this factory in autoQuoteCtrl in autoQuoteCtrl.js file.

But its giving error

angular.js:13424 Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- dtoResource
http://errors.angularjs.org/1.5.3/$injector/unpr?p0=%24resourceProvider%20%3C-%20%24resource%20%3C-NaNtoResource
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:68:12
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:4418:19
at Object.getService [as get] (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:4571:39)
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:4423:45
at getService (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:4571:39)
at injectionArgs (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:4595:58)
at Object.invoke (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:4617:18)
at Object.enforcedReturnValue [as $get] (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:4464:37)
at Object.invoke (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:4625:19)
at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.js:4424:37

My code of controller is as below
autoQuoteCtrl.js

(function () {
"use strict";

angular
.module("autoQuote")
.controller("autoQuoteCtrl", ["$scope","$http","$state","dtoResource", function($scope,$http, $state,dtoResource) {
$scope.onSubmit = function() {
// add check if valid
//call function from your service, and do something with it
dtoResource.rc1Step1DTO();
alert('hi, I was invoked on form submit');
$http({
method : 'POST',
url : 'api.php',
data : { dtoObj: JSON.stringify(prepareAutoQuoteDTO.postAutoQuoteObj) }, // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
})
.success(function(data) {
console.log(data);
if (!data.success) {

} else {
// if successful, bind success message to message
//$scope.message = data.message;
}
});
};
}]);
}());


What I am missing here.
Created plunker with complete code.
http://plnkr.co/edit/NEmPfu18tePMZ8keZp2p?p=preview

Answer

You need to include the angular-resource script. It's in a different module.

Here's a cdn reference you can use if you want.

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular-resource.min.js"></script>

And make sure you add it as a dependency to your module:

var app = angular.module("autoQuote",["ui.router", "ngResource"]);