相关疑难解决方法(0)

如何检查ng-content是否存在

假设我有简单的Bootstrap面板组件,带有多个转换插槽.模板示例:

<div class="panel panel-default">
  <div class="panel-heading">
    <ng-content select="my-panel-heading"></ng-content>
  </div>
  <div class="panel-body">
    <ng-content select="my-panel-content"></ng-content>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我想让panel-heading可选.<div class="panel-heading">如果没有提供内容,我如何隐藏元素<ng-content select="my-panel-heading"></ng-content>

transclusion angular

20
推荐指数
2
解决办法
8790
查看次数

如果ng-content具有内容或空Angular2

我试图了解如何在a ng-content为空时创建if来显示.

<div #contentWrapper [hidden]="isOpen">
    <ng-content ></ng-content>
</div>

<span *ngIf="contentWrapper.childNodes.length == 0">
    <p>Display this if ng-content is empty!</p>
</span>
Run Code Online (Sandbox Code Playgroud)

当内容为空时我尝试使用那个显示数据,但即使信息为空也不会显示<span>标签

谢谢,永远感谢你的帮助.

angular2-template angular

11
推荐指数
1
解决办法
5816
查看次数

是否可以将默认值设置为&lt;ng-content&gt;

我只是从本教程中学习Transluctionin 的用法angular2

https://scotch.io/tutorials/angular-2-transclusion-using-ng-content

我可以将<ng-content>标记用于某些内容,例如:

<ng-content select="[my-block-header]"></ng-content>
Run Code Online (Sandbox Code Playgroud)

它位于my-block附加选择器的组件中。

它将来自其他组件的内容呈现为:

<my-block>
    <div class="box-header" my-block-header> <--added the slot selector here
        <h3 class="box-title">
            My title
        </h3>
    </div>
</my-block>
Run Code Online (Sandbox Code Playgroud)

问题是:

<ng-content>如果我们没有传递任何值,是否可以将默认值添加到可以使用的块中?

到目前为止,如果没有传递任何值,则该位置将出现空白页。

编辑:

当我尝试测试时,有一个较新的版本zone.js不允许显示正确的错误,因此我得到的错误为:

Cannot set property 'stack' of undefined ; Zone: <root> ; 
Task: Promise.then ; Value: TypeError: Cannot set property 'stack' of undefined
Run Code Online (Sandbox Code Playgroud)

但是,当我改变的版本zone.js0.7.2错误控制台作为清楚地提到:

zone.js:392 Unhandled Promise rejection: Template parse errors:
<ng-content> element cannot have content.
Run Code Online (Sandbox Code Playgroud)

因此,它确认没有任何默认值设置为 <ng-content>

default-value angular2-ngcontent angular

7
推荐指数
3
解决办法
2051
查看次数

如何为Angular中的空ng内容提供后备广告?

说,我有一个在其模板中接受ng-content的组件:

但是我希望该组件显示一些默认内容作为后备,以防万一没有提供任何ng-content。是否可以使用* ngIf类型的指令来测试ng-content是否为空?

angular

4
推荐指数
1
解决办法
2869
查看次数