我想在组件中向Controller添加依赖项:
这是我的组件:
angular
.module('app')
.component('myComponent', {
template: '<div>some content</div>',
controller: ["$routeParams", function ($routeParams) {
var ctrl = this;
ctrl.myId = $routeParams.id;
});
Run Code Online (Sandbox Code Playgroud)
现在我只想测试如下:
describe("spec for myComponent", function () {
var $componentController;
var $routeParams;
beforeEach(module('app'));
beforeEach(inject(function (_$componentController_, _$routeParams_) {
$componentController = _$componentController_;
$routeParams = _$routeParams_;
$routeParams = {
myId: 42
}
}));
it('should init', function () {
var ctrl = $componentController('myComponent', $routeParams, null);
expect(ctrl.myId).toBe(42);
});
});
Run Code Online (Sandbox Code Playgroud)
但遗憾的是ctrl.myId未定义,因为没有正确注入$ routeParams.为什么?