【问题标题】:TS Observable & ng: proper templating syntaxTS Observable & ng:正确的模板语法
【发布时间】:2017-04-03 07:42:59
【问题描述】:

我有一个可观察到的“applicationDetail”(private applicationDetail: Observable<ApplicationDetail>;)。我像这样绑定值:

<h2 class="detailViewH2">{{ (applicationDetail | async)?.AttendeeName }}</h2>
<p>{{ (applicationDetail | async)?.AttendeeFunction }} </p>

这对我来说似乎不是正确的模板语法,有没有办法通常说“applicationDetail”将是异步的,并且它的所有道具都应该使用安全导航运算符?

【问题讨论】:

    标签: angular typescript observable templating


    【解决方案1】:

    在 Angular4 中,您可以使用 *ngIf 解决此问题

    <ng-container *ngIf="(applicationDetail | async) as foo">
      <h2 class="detailViewH2">{{foo.AttendeeName }}</h2>
      <p>{{foo.AttendeeFunction }} </p>
    </ng-container>
    

    您还可以创建自己的结构指令,当表达式为 false 时不会删除内容,但会提供此功能。

    【讨论】:

    • 是的,仅限 AFAIK V4+。
    • 你知道如何将它与两个变量一起使用吗?使用(expr1) &amp;&amp; (expr2) 不起作用。使用两个ng-containers 的作品,虽然很丑陋。
    • 也许&lt;ng-container *ngIf="{a: applicationDetail | async, b: bar | async } as foo"&gt;{{foo.a.AttendeeName}} {{foo.b.bar}}&lt;/div&gt;。虽然还没有尝试过自己。这也将阻止*ngIf 删除内容,因为该值始终是真实的。
    • 感谢您的反馈 :)
    猜你喜欢
    • 2018-02-14
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多