我似乎无法使角度模板自动完成工作,任何人都可以帮助我理解我错过了什么或做错了什么吗?
<div *ngVar="ok as xd">
<!-- xd is not giving any autocomplete / typecheck, hover giving "any" type -->
{{ xd.item }}
</div>
我几乎继续研究 ngIf 和异步管道源代码,试图了解正在发生的事情,并制定了这个简单的指令,只是为了方便我的异步订阅生活(这对 ngIf 来说有点痛苦)。
没有类型安全同样是一种痛苦 x)
type NgVarContext<T> = { ngVar: T; $implicit: undefined };
@Directive({
selector: "[ngVar]",
})
export class NgVar<T> {
context: NgVarContext<T> = { ngVar: undefined, $implicit: undefined };
_subscription: Subscription;
constructor(
private cdr: ChangeDetectorRef,
template: TemplateRef<NgVarContext<T>>,
viewContainer: ViewContainerRef
) {
viewContainer.createEmbeddedView(template, this.context);
}
@Input()
set ngVar(input: Observable<T>) {
if (this._subscription) return;
this._subscription = input.subscribe((item) => {
if (item !== this.context.ngVar) {
this.context.ngVar = item;
this.cdr.markForCheck();
}
});
}
static ngTemplateGuard_ngVar: "binding";
static ngTemplateContextGuard<T>(dir: NgVar<T>, ctx: NgVarContext<T>): ctx is NgVarContext<T> {
return true;
}
}
只是如果您不知道(将其写为答案,因为我无法以
See: https://twitter.com/angular/status/1357403631901937672 https://twitter.com/angular/status/1357403631901937672
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)