javascript - 错误: $injector:unpr Unknown Provider Angular

标签 javascript angularjs asp.net-mvc angularjs-service iife

我在布局中创建了一个项目 MVC(用于加载菜单类别):

<html data-ng-app="app">
.
.
.
//in the menu
<li class="dropdown" ng-controller="menuCategoriesCtrl as vmCat">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">Categorias<span class="caret"></span> 
  </a>
  <ul id="listaCategorias" class="dropdown-menu" aria-labelledby="dropdownMenu1" ng-repeat="categoria in vmCat.categories">
    <li>
      <a ng-href="{{categoria.url}}">
        {{categoria.Nombre}}
      </a>
    </li>
  </ul>
 </li>

app.js 是这样的:

(function () {

    "use strict";

    angular
        .module('app', ['ngRoute', 'ngAnimate', 'ui.bootstrap']).config(configRoute);

    configRoute.$inject = ['$routeProvider', '$locationProvider'];

    function configRoute($routeProvider, $locationProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'scripts/app/partials/Home.html',
                controller: 'productosPrincipalCtrl',
                controllerAs: 'vm'
            })
            .when('/Mapa', {
                templateUrl: 'scripts/app/partials/Map.html'
            })
            .otherwise({
            redirectTo: '/'
        });

        $locationProvider.html5Mode(false); //.hashPrefix("!")
    }

})();

菜单类别的 Controller 是这样的:

(function() {

    angular.module('app')
        .controller('menuCategoriesCtrl', menuCategoriesCtrl);

    menuCategoriesCtrl.$inject = ['categoryService'];

    function menuCategoriesCtrl(categoryService) {

        var vmCategory = this;
        var listCat = [];

        vmCategory.listCategories = [];

        getListCategories().then(function() {

            for (var i = 0; i < listCat.length; i++) {
                vmCategory.listCategories.push({
                    url: '#/Categoria/' + listCat.CategoriaId + '-' + listCat.Nombre,
                    nombreCategoria: listCat.Nombre
                });
            }

        });

        function getListCategories() {
            return categoryService.getCategories()
                .then(function(response) {
                    listCat = response;
                    return listCat;
                })
                .catch(function (response) {
                    return alert('Error ' + response);
                });
        };

    }

})();

服务是这样的:

(function() {

    var uriCategorias = 'http://localhost/Api/GetCategories';

    angular.module('app')
        .service('categoryService', categoryService);

    categoryService.$inject = ['$http'];

    function categoryService($http) {

        var srvCat = this;

        srvCat.getCategories = getCategories;

        function getCategories() {

            return $http.get(uriCategorias)
                .then(getCategoriesComplete)
                .cath(getCategoriesFail);

            function getCategoriesComplete(response) {
                return response.data;
            }

            function getCategoriesFail(response) {
                return alert('Error ' + response);
            }
        }

    }
})

当我在浏览器中执行此操作时,我在服务中的 Controller 中注入(inject)时出现错误。

有人可以解释一下为什么吗?

名称是正确的,我在 app_start 的 bundle 中引用了所有内容

提前致谢

最佳答案

您应该调用服务函数,因为您正在使用自执行函数(IIFE 模式)。因为service.js的函数没有被调用,所以它没有将service组件绑定(bind)到app模块。因此,在代码中注入(inject)服务会出现错误,因为它未在应用程序模块中注册。

代码

(function() {

    //service code as is

})(); <-- () self calling function should be there

关于javascript - 错误: $injector:unpr Unknown Provider Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34382879/

相关文章:

javascript - 如何在单击时将两个单独的事件分配给 div 标签

angularjs - 如何从 Controller 动态更改 Angularjs 服务内部变量的值?

AngularJS:测试 $resource PUT 请求

c# - ASP .NET 5 MVC 6 beta 7 升级 : "Multiple assemblies with equivalent identity" issue

javascript - 多次替换一个字母

javascript - jQuery 事件中的递归

AngularJs浏览器后退按钮: Go Back to Previous Spot on Page

javascript - MVC 4 仅刷新 PartialView

asp.net-mvc - 如何将多选列表选定的项目传递回 Controller ?

javascript - 编写惰性 ajax 服务的最佳实践