angular - 在处理组件中的 nativeElement.setAttribute 的函数中升级到 Angular6 后出现 TypeError
问题描述
问题!
我有一个搜索栏组件,它有一个 textarea 作为搜索字段。根据我搜索的文本数量,我增加或减少 textarea 本身的行数。所以我有一些处理文本区域的本机元素的操作。例如这个函数
搜索栏.ts
focus() {
if (this.textArea) {
this.focussed = true;
this.textArea.nativeElement.setAttribute('rows', this.calcRows());
// extra part ...
}
}
搜索栏.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',TypeError:未定义不是函数(评估'this.textArea.nativeElement.setAttribute('rows',this.calcRows())')
任何想法如何解决它?提前致谢
解决方案
尝试this.textArea.nativeElement.rows = this.calcRows()
推荐阅读
- c# - 从 WebApi 中的 HttpRequestMessage.Content 检索的流中检索数据
- oracle - 存储过程中的 Oracle 搜索
- java - List 类型中的方法 get(int)
不适用于 Java 8 中的参数字符串 - html - 为什么html元素的溢出设置比body元素更重要?
- java - 我有 4 个“未解决的要求”,但在我的 build.gradle 中我没有这种类型的依赖项
- google-app-maker - 将值绑定到 List Widget 中的多个标签;使用动态页面属性
- node.js - 如何在同一台机器上处理不同的请求 url?
- eclipse - 与 Maven 相比,Eclipse Java 11 类路径仅不起作用
- laravel-backpack - 无法使用基于角色的中间件限制对权限管理器路由的访问
- php - laravel 5 - 通过 foreach 更改标签 ID