我正在编写一个具有属性的Angular组件Mode(): string.我希望能够以编程方式设置此属性,而不是响应任何事件.问题是在没有浏览器事件的情况下,模板绑定{{Mode}}不会更新.有没有办法手动触发此更改检测?
我有点困惑为什么ngAfterContentInit在这种情况下执行两次.我已经创建了我们的应用程序的精简版本来重现该错误.简而言之,我使用a *contentItem来标记组件,然后由standard-layout组件拾取以进行渲染.只要我遵循这个模式,demo的ngAfterContentInit执行两次.
我将演示应用程序放在github上,这将重现错误:https: //github.com/jVaaS/stackoverflow/tree/master/ngaftercontentinit
否则这里是重要的一点:
越野车,app.dart:
@Component(
selector: "buggy-app",
template: """
<standard-layout>
<demo *contentItem></demo>
</standard-layout>
""",
directives: const [
ContentItemDirective,
StandardLayout,
Demo
]
)
class BuggyApp implements AfterContentInit {
@override
ngAfterContentInit() {
print(">>> ngAfterContentInit: BuggyApp");
}
}
Run Code Online (Sandbox Code Playgroud)
标准layout.dart:
////////////////////////////////////////////////////////////////////////////////
///
/// Standard Layout Component
/// <standard-layout></standard-layout>
///
@Component(
selector: "standard-layout",
template: """
<div *ngFor="let item of contentItems ?? []">
<template [ngTemplateOutlet]="item.template"></template>
</div>
""",
directives: const [ROUTER_DIRECTIVES, ContentItem])
class StandardLayout implements AfterContentInit …Run Code Online (Sandbox Code Playgroud)