简体   繁体   English

错误:$ injector:unpr未知提供程序角度

[英]Error: $injector:unpr Unknown Provider Angular

I have create a project MVC in the layout i have this (for load Menu Categories): 我已经在布局中创建了一个项目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> 

the app.js is this: 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("!") } })(); 

the controller for the menu Categories is this: 菜单类别的控制器是这样的:

 (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); }); }; } })(); 

and the service is this: 服务是这样的:

 (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); } } } }) 

when i execute this in the browser i have a error for the inject in controller in the service. 当我在浏览器中执行此操作时,服务中的Inject in控制器出现错误。

Can somebody explain me why? 有人可以解释一下为什么吗?

the name is correct i have reference all in the bundle in the app_start 名称正确无误,我已经引用了app_start中的所有捆绑包

thanks in advance 提前致谢

You should call the service function, as you are using Self Executing function(IIFE pattern). 您应该像使用自执行功能(IIFE模式)那样调用服务功能。 Because the function of service.js not called, it doesn't bind the service component to app module. 由于未调用service.js函数,因此不会将service组件绑定到app模块。 Hence injecting service in your code giving error as its not registered with app module. 因此,在代码中注入服务会导致错误,因为它未在应用模块中注册。

Code

(function() {

    //service code as is

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM