Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi -

Working with angularjs now and just messing around with first project. I'm getting an unknown provider error. I've looked at the documentation and cant exactly figure out what i'm doing wrong. Actually at this point I've gotten myself confused

Here is the the main controller

JavaScript
(function () {
    "use strict";
    angular
        .module("app", [])        
        .controller("mainCtrl", ["$scope", "myService", mainCtrl]);

    function mainCtrl($scope, myService) {
        var genericObjects =
            [
                { name: "name 1" },
                { name: "name 2" },
                { name: "name 3" },
                { name: "name 4" }
            ];
        $scope.genericObjects = genericObjects;
        $scope.testServiceVariable = myService.MarginAmount(100,80);
    }   

}());


Here is the service
JavaScript
(function () {
    "use strict";
    angular
        .module("app")
        .factory("productService", productService);

    function productService() {
        function MarginPercent(price, cost) {
            var margin = price && cost ? (100 * (price - cost)) / price : 0;
            margin = Math.round(margin);
            return margin;
        }

        function MarginAmount(price, cost) {
            var margin = price && cost ? margin - price : 0;
            return margin;
        }

        return {
            MarginPercent: MarginPercent,
            MarginAmount: MarginAmount
        }

    } //end product service            

}());


What I have tried:

I've been staring at this link https://docs.angularjs.org/error/$injector/unpr?p0=myServiceProvider%20%3C-%20myService%20%3C-%20mainCtrl[^] and switched it around multiple times but keep getting the same error. I've defined the service then passed the service to the function but didn't work either.
Posted
Updated 25-Feb-16 5:40am
v2

1 solution

You are injecting wrong service in a controller. Inject "productService" instead of "myService" in a controller.
Please refer below
Understanding AngularJS Factory, Service and Provider

Hopefully, it will work ...!
 
Share this answer
 
Comments
Troy Bryant 25-Feb-16 9:47am    
I noticed that and did change but still was undefined.
Konstantin A. Magg 25-Feb-16 15:47pm    
Hey Troy,
could you update the code in your question then, please?
What is undefined now? The provider, the service or the MarginAmount function?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900