我不认为添加@ViewChild('greatTemplate) ref;
是一个好的解决方案,只是为了满足测试要求。必须有一种方法可以通过TestBed获得local variable
引用。
<button (click)="doSomething(greatTemplate)>
<ng-template #greatTemplate>Hello, World.</ng-template>
我唯一能通过angular.io文档找到的是here in this section "debugelement",它解释了您可以挖掘夹具的debugElement引用来寻找local variables
,但这似乎只能捕获组件或本机DOM元素,而不是ng-template
。