javascript - Angular 6 创建 TemplateRef 的实例

标签 javascript typescript testing angular6

我有 TemplateRef<any> 的( Angular 6)组件和输入参数类型

class TestComponent {
    @Input() tpl: TemplateRef<any>;
    ...
}

现在我想创建一个测试,但我找不到如何在测试中创建模板并将其设置为输入字段的方法。

在我的 HTML 中我有这样的模板

<test>
    <ng-template #tpl>
        <div>OLOLO</div>
    </ng-template>
</test>

我需要这样的东西

fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
component.template = here I need instance of TemplateRef

谢谢

最佳答案

我不明白你为什么要这样做,但如果你想访问规范文件中的模板,而不是使用输入,请在组件中使用 viewchild

所以使用代码会是

class TestComponent {
  @ViewChild('tpl') public tpl: ElementRef;
  ...
}

在 spec.ts 中试试这个

it('should create', () => {
  console.log(component.tpl);
  expect(component).toBeTruthy();
});

关于javascript - Angular 6 创建 TemplateRef 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655489/

相关文章:

javascript - Knockout.compated - 访问元素属性

javascript - 如何调试 Mustache js 模板?

c# - 在 MVC 中使用 AngularJs 时保持 View 的强类型

node.js - 我无法在 Windows 上使用 "vows test/*"命令运行测试。如何使用它? Node .js

javascript - 如何使用 Selenium 测试图像裁剪(选择)?

javascript - Jquery: Uncaught TypeError: Object #<Object> 没有方法 'quicksand'

JavaScript 日期解析产生错误的日​​期

angular - 属性 "usb"在 angular2 typescript 项目中的类型 "Navigator"上不存在

javascript - Typescript 加载模块通过 AMD 失败

docker - 在做 CI 运行测试时,我应该使用 production docker build 吗?