【发布时间】:2017-08-31 16:10:46
【问题描述】:
我目前的测试代码有问题。 一个例子胜过一万个字所以就在这里。
describe('Initilazing', () => {
let $componentController, scope;
beforeEach(inject((_$componentController_, _$rootScope_) => {
$componentController = _$componentController_;
scope = _$rootScope_.$new();
scope.name = 'Testing...';
}));
it('Should have the parent scope double binded', () => {
let $ctrl = $componentController('sasInput', scope, { api: scope.sasInput });
$ctrl.onInit();
scope.$apply();
expect(scope.sasInput).not.toBeUndefined();
});
});
class SasInputController() {
constructor() {
'ngInject';
}
$onInit() {
this.api = { clear: this.clear.bind(this) };
}
clear() {
this.input = null;
}
}
export default {
...
bindings: {
api: '='
}
}
这是交易,在我的组件中,scope.sasInput 应该使用 $onInit 上的当前组件 api 进行更新。但事实并非如此。 我在这里做错了什么?
你能帮忙吗? 谢谢。
【问题讨论】:
-
能否请您显示您的组件定义和
$onInit组件控制器的处理程序? -
已编辑。为清楚起见,我没有放置所有导出参数。绑定是这里有趣的一个。
-
在执行
this.api = { clear: ...时,您将失去对传递给组件的scope对象的引用。使用更复杂的模型可以帮助您避免这种情况。看看this gist with mentioned approach,希望对你有帮助。 -
我明天上班试试这个。我一测试这个就回来找你:)如果这可行,我不明白为什么tbh。因为代码在浏览器中完美运行。