如何从 Angular2 DebugElement 访问范围数据?
How do I access the scope data from an Angular2 DebugElement?
我想循环调试 DebugElements 以确保元素描述的对象具有某些属性。例如,我可能想确保显示仅显示今天有预约的患者,而不是可用患者的完整列表。
如何从调试元素访问范围数据?
例如:
注意:在下面的代码中,page
变量将频繁的调试元素搜索打包到单个 class 中。在这种情况下,它为同一列表组件的两个实现提供调试元素,并且每个列表组件根据与该问题无关的标准显示不同的患者列表。
it( "lists zero patients from other staff members that the staff member who is logged in", ()=>{
var element : DebugElement, list : any;
var user : string = component.credentials.username;
var notMyPatientCount : number = 0;
for (list of [page.primaryPatients, page.patientBacklog] ){
for( element of list ){
var patient = /* I need something to put here to extract the PatientSummary object that is displayed in this element */;
}
}
expect( notMyPatientCount ).toBe( 0, "When filtered, the display only holds patients assigned to the current user." );
});
测试页面具有 DebugElement (click here for DebugElement API) 的 API 引用。
我之前查看过此文档,但我忽略了一个事实,即名为 "componentInstance" 的属性指的是附加到调试元素而不是测试范围的组件实例。
为了访问 DebugElement 中使用的 PatientSummary 对象,我使用了以下代码:
/**
*
* @Component( ... )
* export class PatientListItemComponent {
* ...
* patientSummary : PatientSummary;
* ...
* }
*
*/
var component : PatientListItemComponent = element.componentInstance;
var patient : PatientSummary = component.patientSummary;
我想循环调试 DebugElements 以确保元素描述的对象具有某些属性。例如,我可能想确保显示仅显示今天有预约的患者,而不是可用患者的完整列表。
如何从调试元素访问范围数据?
例如:
注意:在下面的代码中,page
变量将频繁的调试元素搜索打包到单个 class 中。在这种情况下,它为同一列表组件的两个实现提供调试元素,并且每个列表组件根据与该问题无关的标准显示不同的患者列表。
it( "lists zero patients from other staff members that the staff member who is logged in", ()=>{
var element : DebugElement, list : any;
var user : string = component.credentials.username;
var notMyPatientCount : number = 0;
for (list of [page.primaryPatients, page.patientBacklog] ){
for( element of list ){
var patient = /* I need something to put here to extract the PatientSummary object that is displayed in this element */;
}
}
expect( notMyPatientCount ).toBe( 0, "When filtered, the display only holds patients assigned to the current user." );
});
测试页面具有 DebugElement (click here for DebugElement API) 的 API 引用。
我之前查看过此文档,但我忽略了一个事实,即名为 "componentInstance" 的属性指的是附加到调试元素而不是测试范围的组件实例。
为了访问 DebugElement 中使用的 PatientSummary 对象,我使用了以下代码:
/**
*
* @Component( ... )
* export class PatientListItemComponent {
* ...
* patientSummary : PatientSummary;
* ...
* }
*
*/
var component : PatientListItemComponent = element.componentInstance;
var patient : PatientSummary = component.patientSummary;