【问题标题】:In Angular.js, is it possible to replace an ng-switch directive element with its contents?在 Angular.js 中,是否可以将 ng-switch 指令元素替换为其内容?
【发布时间】:2013-06-01 04:35:45
【问题描述】:

我正在使用一个开关来有条件地包含不同的内容。我希望内部内容(例如欢迎指令)替换父 ng-switch 元素。我知道你可以通过使用 replace=true 配置属性的自定义指令来做到这一点,但这是否可以通过 ng-switch 等内置指令来实现?

<div id="container">
    <ng-switch on="tabIndex">

        <welcome ng-switch-when="welcome"></welcome>

        <main ng-switch-when="main"></main>

        <help ng-switch-when="help"></help>

    </ng-switch>
</div>

例如,当tabIndex的值为'help'时,我希望得到以下html:

<div id="container">

    <help><!-- contents of help template --></help>

</div>

【问题讨论】:

  • 我认为仅使用 ng-switch 指令是不可能的。它只是将正确的 switch-when 附加到其主要元素。

标签: angularjs angularjs-directive ng-switch


【解决方案1】:

你总是需要那里的逻辑,但你没有必须使用一个元素进行切换。它与父级的属性一样有效:

<div id="container" ng-switch on="tabIndex">
    <welcome ng-switch-when="welcome"></welcome>
    <main ng-switch-when="main"></main>
    <help ng-switch-when="help"></help>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    相关资源
    最近更新 更多