我在将控制器转换为为 Angular 2 准备我的应用程序的组件时遇到了问题,但问题是迁移不顺利,我有 ui-router 在状态之间路由并在一些控制器中使用解析,带有控制器的版本正在工作,但组件的版本现在完全可以工作,我遵循了很多指南,似乎我在代码方面做得很好,但对我不起作用。
我有以下带有控制器的模块:
(function () {
'use strict';
angular
.module('app.sample', [])
.config(config);
/** @ngInject */
$stateProvider
.state('app.sample', {
url : '/sample',
views : {
'content@app': {
templateUrl: 'app/main/sample/sample.html',
controller : 'SampleController as vm'
}
},
resolve: {
SampleData: function (myService) {
return myService.getSample(); // I return a promise here
}
}
});
}
})();
控制器:
(function ()
{
'use strict';
angular
.module('app.sample')
.controller('SampleController', SampleController);
/** @ngInject */
function SampleController(SampleData)
{
var vm = this;
vm.helloText = SampleData.data.helloText;
}
})();
上面的代码运行良好,但是在将其作为一个组件后,它变成了这样:
(function () {
'use strict';
angular
.module('app.sample', [])
.config(config);
/** @ngInject */
function config($stateProvider) {
// State
$stateProvider
.state('app.sample', {
url: '/sample',
views: {
'content@app': {
template: '<sample></sample>'
}
},
resolve: {
SampleData: function (myService) {
return myService.getSample(); // I return a promise here
}
}
});
}
})();
组件:
(function () {
'use strict';
angular
.module('app.sample')
.component('sample', {
templateUrl: 'app/main/sample/sample.html',
bindings: {
},
controller: Sample
});
/** @ngInject */
function Sample(SampleData) {
var $ctrl = this;
$ctrl.helloText = SampleData.data.helloText;
}
})();
但现在它不起作用并给我以下错误:
Error: [$injector:unpr] Unknown provider: SampleDataProvider <- SampleData
http://errors.angularjs.org/1.5.7/$injector/unpr?p0=SampleDataProvider%20%3C-%20SampleData
at angular.js:68
at angular.js:4502
at Object.getService [as get] (angular.js:4655)
at angular.js:4507
at getService (angular.js:4655)
at injectionArgs (angular.js:4679)
at Object.invoke (angular.js:4701)
at $controllerInit (angular.js:10234)
at nodeLinkFn (angular.js:9147)
at angular.js:9553
我在bower.json 中的依赖项:
"dependencies": {
"angular": "1.5.7",
"angular-animate": "1.5.7",
"angular-aria": "1.5.7",
"angular-cookies": "1.5.7",
"angular-material": "1.1.0-rc.5",
"angular-messages": "1.5.7",
"angular-resource": "1.5.7",
"angular-sanitize": "1.5.7",
"angular-ui-router": "1.0.0-beta.1",
"jquery": "2.2.4",
"mobile-detect": "1.3.2",
"moment": "2.13.0"
}
知道什么问题,我错过了什么吗?