向控制器注入服务依赖项时出错 [英] Error while injecting service dependency to controller
问题描述
我已经创建了一项服务,并且想向控制器注入依赖项,但是它显示了一些错误,例如
I have created one service and I want to inject dependency to controller, but it showing some error like
angular.js:12477错误:[$ injector:unpr]未知提供程序:ResultProvider<-结果<-ToolbarControllerhttp://errors.angularjs.org/1.4.7/$injector/unpr?p0=ResultProvider%20%3C-%20Result%20%3C-%20ToolbarController在http://localhost:3000/bower_components/angular/angular.js:68:12在http://localhost:3000/bower_components/angular/angular.js:4289:19at Object.getService [as get](http://localhost:3000/bower_components/angular/angular.js:4437:39)在http://localhost:3000/bower_components/angular/angular.js:4294:45在getService(http://localhost:3000/bower_components/angular/angular.js:4437:39)在调用时(http://localhost:3000/bower_components/angular/angular.js:4469:13)在Object.instantiate(http://localhost:3000/bower_components/angular/angular.js:4486:27)在http://localhost:3000/bower_components/angular/angular.js:9151:28在http://localhost:3000/bower_components/angular-ui-router/release/angular-ui-router.js:4018:28在invokeLinkFn(http://localhost:3000/bower_components/angular/angular.js:8789:9)< md-toolbar id ="toolbar" class ="md-menu-toolbar md-whiteframe-1dp md-background-bg ng-scope"ui-view =" toolbar>
api.sharedResult = $resource(api.dataUrl + 'sharedResult/sharedResult.json', null, {
get: {method: 'get'}
});
在模块中,我写得像
(function ()
{
'use strict';
angular
.module('app.toolbar', [])
.config(config);
/** @ngInject */
function config($stateProvider, $translatePartialLoaderProvider)
{
$translatePartialLoaderProvider.addPart('app/toolbar');
$stateProvider.state('app.toolbar', {
url : '/sharedwishlistdetails',
views : {
'content@app': {
templateUrl: 'app/toolbar/sharedwishlistdetails.html',
controller : 'ToolbarController as vm'
}
},
resolve: {
Result: function (apiResolver)
{
return apiResolver.resolve('sharedResult@get');
}
}
});
}
})();
在控制器中,我像下面这样注入
In controller I have injected like below
(function ()
{
'use strict';
angular
.module('app.toolbar')
.controller('ToolbarController', ToolbarController);
function ToolbarController(Result, $rootScope, $mdSidenav, msNavFoldService, $translate, $mdToast, $location, $localStorage, $http, $scope)
{
var vm = this;
vm.sharedResult = Sharedresult.sharedResult;
}
})();
sharedwithlistdetails.html
<div id="sharedwishlistdetails" class="page-layout simple tabbed">
<!-- HEADER -->
<div class="header md-accent-bg" layout="row" layout-align="start center">
<div class="title">
<span class="md-display-1 font-weight-300">Shared Wishlist Details</span>
</div>
</div>
<!-- / HEADER -->
<md-content>
<div class="head">
<h2>Shared Wishlist Details</h2>
</div>
<hr/ style="width:1000px">
</md-content>
<md-content layout-padding>
<div ng-repeat="data in vm.rslt">
<h2>{{data.wName}}</h2>
</div>
</md-content>
</div>
推荐答案
错误消息的这一行:
at invokeLinkFn( http://localhost:3000/bower_components/angular/angular.js:8789:9 )
<md-toolbar id="toolbar" ... ui-view="toolbar">
表示 ui-view
已设置为指定状态.
indicates the ui-view
has been set with a specified state.
您需要将其更改为:
<md-toolbar ... ui-view>
这篇关于向控制器注入服务依赖项时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!