本文介绍了我如何注入这个angularjs服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 -



现在使用angularjs,只是搞乱第一个项目。我收到了一个未知的提供程序错误。我查看了文档,无法弄清楚我做错了什么。实际上在这一点上我已经让自己困惑了



这是主控制器



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

(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

(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.

推荐答案




这篇关于我如何注入这个angularjs服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:28