我的项目中同时具有属性和结构指令。我可以通过创建测试组件并在模板中使用attribute指令来测试attribute指令。
@Component({
template: `<input [myAttrDir]="{prop1: val1, prop2: val2}"/>`
})
export class TestComponent {
}
@Directive({
selector: '[myAttrDir]'
})
export class MyAttrDirective {
@Input('myAttrDir') testProp;
}
Run Code Online (Sandbox Code Playgroud)
测试模块如下所示:
TestBed.configureTestingModule({
declarations: [MyAttrDirective, TestComponent]
})
Run Code Online (Sandbox Code Playgroud)
我这样掌握指令:
fixture = TestBed.createComponent(TestComponent)
directive = fixture.debugElement.query(By.directive(MyAttrDirective))
Run Code Online (Sandbox Code Playgroud)
我能够获得属性指令的实例。但是,当我尝试以相同的方式测试结构化指令时,会得到指令的空值。我也检查了官方文档,仅发现attribute指令的单元测试。在任何地方都没有给出结构指令测试方法。
@Component({
template: `<input *myStrucDir="{prop1: val1, prop2: val2}"/>`
})
export class TestComponent {
}
@Directive({
selector: '[myStrucDir]'
})
export class MyStrucDirective {
@Input set myStrucDir(data);
constructor(
private templateRef: TemplateRef<any>,
private vcr: ViewContainerRef,
private cfr: ComponentFactoryResolver,
private el: ElementRef) {
}
}
TestBed.configureTestingModule({ …Run Code Online (Sandbox Code Playgroud) 我正在使用angular 5和rxjs。我正在拨打2个服务电话,其中一个取决于其他结果。我正在使用flatMap。我也在使用takeUntil,以便可以在任何给定点中止操作。我的代码如下所示:
this.myservice.api1(param1).pipe(takeUntil(this.destroyed$), finalize(() => {
//do something after both api calls are completed
},
flatMap((result1) => {
//do some operation and create object x(this.objx)
return this.myservice.api2(param1);
})
).subscribe((result2) => {
//do something based on result2 and this.objx
})
Run Code Online (Sandbox Code Playgroud)
该代码在for循环中执行,循环执行200次。因此,进行了400个网络呼叫。我在UI上有一个按钮来中止此操作,单击它时将执行this.destroyed $ observable。对于未进行第二次API调用的任何迭代,我都可以中止这种方式(api2)。如果只调用第一个API,该请求将被取消。如果api2被调用,需要20到30秒的响应时间,它不会被取消。我希望两个API调用都被取消。可能吗?
我有一个删除操作员代码片段,如下所示:
(function() {
var objA = Object.create({
foo: 'foo'
});
var objB = objA;
objB.foo = 'bar';
delete objA.foo;
console.log(objA.foo);
console.log(objB.foo);
}());Run Code Online (Sandbox Code Playgroud)
//it logs-> foo
Run Code Online (Sandbox Code Playgroud)
由于delete运算符用于删除foo属性,因此它不应该存在,因此undefined应该在控制台中登录.然而,它是foo在初始化时记录的属性值.为什么不删除该属性?
我有一个接受两个参数的函数,每个参数都是HTML元素类型。应该返回哪个元素首先出现在文档顺序中。有没有简单的方法可以确定这一点?
范本-
<body>
<div id="div1">
<div id="div2">
</div>
</div>
<div id="div3">
<div id="div4">
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
JS-
const elem1 = document.getElementById('div2');
const elem2 = document.getElementById('div4');
const firstAppearingElement = checkOrder(elem1, elem2); // it should return elem1
function checkOrder(element1, element2) {
// check which one appears first in dom tree
}
Run Code Online (Sandbox Code Playgroud)