在组件中处理 nativeElement.setAttribute 的函数中升级到 Angular6 后出现 TypeError
TypeError after upgrading to Angular6 in function dealing with nativeElement.setAttribute in component
问题!
我有一个搜索栏组件,它有一个文本区域作为搜索字段。
根据我搜索的文本数量,我增加或减少文本区域本身的行数。
所以我有一些处理文本区域的本机元素的操作。
例如这个函数
searchbar.ts
focus() {
if (this.textArea) {
this.focussed = true;
this.textArea.nativeElement.setAttribute('rows', this.calcRows());
// extra part ...
}
}
searcbar.spec.ts
describe('focus', () => {
it('should set focussed to true', () => {
component.focussed = false;
component.focus();
expect(component.focussed).toBe(true);
});
it('should set the height of the textArea by setting the row to the correct amount', () => {
spyOn(component.textArea.nativeElement, 'setAttribute');
spyOn(component, 'calcRows').and.returnValue(2);
component.focus();
expect(component.textArea.nativeElement.setAttribute).toHaveBeenCalledWith('rows', 2);
});
});
测试实际上并没有失败。
但是我显示了这个错误,我认为它们不是指测试本身,而是指组件。
ERROR: 'ERROR', TypeError: undefined is not a function (evaluating
'this.textArea.nativeElement.setAttribute('rows',this.calcRows())')
有什么解决办法吗?
提前致谢
尝试this.textArea.nativeElement.rows = this.calcRows()
问题!
我有一个搜索栏组件,它有一个文本区域作为搜索字段。 根据我搜索的文本数量,我增加或减少文本区域本身的行数。 所以我有一些处理文本区域的本机元素的操作。 例如这个函数
searchbar.ts
focus() {
if (this.textArea) {
this.focussed = true;
this.textArea.nativeElement.setAttribute('rows', this.calcRows());
// extra part ...
}
}
searcbar.spec.ts
describe('focus', () => {
it('should set focussed to true', () => {
component.focussed = false;
component.focus();
expect(component.focussed).toBe(true);
});
it('should set the height of the textArea by setting the row to the correct amount', () => {
spyOn(component.textArea.nativeElement, 'setAttribute');
spyOn(component, 'calcRows').and.returnValue(2);
component.focus();
expect(component.textArea.nativeElement.setAttribute).toHaveBeenCalledWith('rows', 2);
});
});
测试实际上并没有失败。 但是我显示了这个错误,我认为它们不是指测试本身,而是指组件。
ERROR: 'ERROR', TypeError: undefined is not a function (evaluating 'this.textArea.nativeElement.setAttribute('rows',this.calcRows())')
有什么解决办法吗? 提前致谢
尝试this.textArea.nativeElement.rows = this.calcRows()