Angularjs 获取错误“;由于提供程序未知,无法实例化模块应用程序;使用我的自定义提供程序
正在尝试创建自定义提供程序并在应用程序配置期间使用它。在配置阶段,出现错误“由于未知的提供程序'ConfigRouteProvider',无法实例化模块应用程序”。 提供商代码:Angularjs 获取错误“;由于提供程序未知,无法实例化模块应用程序;使用我的自定义提供程序,angularjs,Angularjs,正在尝试创建自定义提供程序并在应用程序配置期间使用它。在配置阶段,出现错误“由于未知的提供程序'ConfigRouteProvider',无法实例化模块应用程序”。 提供商代码: (function () { 'use strict'; angular.module('app-routing', []) .provider('configRoutesProvider', function () { this.$get = ['$
(function () {
'use strict';
angular.module('app-routing', [])
.provider('configRoutesProvider', function () {
this.$get = ['$http', getRoutesProvider];
function getRoutesProvider($http) {
return {
getRoutes: function ($http) {
return $http.get('https://localhost:44311/api/account/routes').then(function (results) {
return results;
});
}
};
}
});
}).call(this);
在app.js代码中,获取对“应用程序路由”模块的引用:
(function () {
'use strict';
var app = angular.module('app',
[
'ngRoute', // routing
'LocalStorageModule', // local storage
'app-routing'
]);
app.run();
})();
在config.js中,当尝试引用提供程序时,会出现上述错误:
app.config(['$routeProvider', 'configRoutesProvider', routeConfigurator]);
function routeConfigurator($routeProvider, configRoutesProvider) {
var routes = configRoutesProvider.getRoutes();
routes.forEach(function (r) {
$routeProvider.when(r.href, {
controller: r.controller,
templateUrl: r.templateUrl
});
});
$routeProvider.otherwise({ redirectTo: '/home' });
}
在index.html中,脚本注册的顺序如下:
<script src="app/routesProvider.js"></script>
<!-- Load app main script -->
<script src="app/app.js"></script>
<script src="app/config.js"></script>
无法理解我错过了什么?在angular中,提供者的名称上没有
提供者
部分,因此我们的想法是:
angular.module('foo').provider('configRoutes', function() { });
而不是:
angular.module('foo').provider('configRoutesProvider', function() { });
但当您将其注入配置时,必须将其:
angular.module('foo').config(function(configRoutesProvider) { });
因此,在您的示例中,删除定义中的
提供者部分。文件:route.js
var appRoutes = angular.module('appRoutes',['ngRoute'])
appRoutes.config(function($routeProvider,$locationProvider){
$routeProvider
.when('/',{
templateUrl: 'app/views/pages/home.html'
})
.when('/about',{
templateUrl: 'app/views/pages/about.html'
})
.otherwise({ redirectTo: '/'});
console.log('Testing routes routes...');
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
});
});
它的作品。
这是在route.js文件中定义路由的另一种方法。
在这里,我正确地给出了路径,并使用$routeProvider、$locationPRovider、ngroute非常感谢!!!它起作用了。我对AngularJS非常陌生,我在documentation.Wow中没有看到这一点。这很痛苦。所以,为了将一个服务更改为提供者,我必须在注入该服务的所有时间查找它?啊,回答得好!这是完全不直观的,但-有没有人有一个链接,它出现在文档中?我将继续,并假设这是相同的服务