【问题标题】:How to define place holder in child component and import the other child component in parent component如何在子组件中定义占位符并在父组件中导入其他子组件
【发布时间】:2022-07-21 16:06:27
【问题描述】:

我有一个关于角度子组件和父组件的问题。

我的想法是:

Parent component: 

<parent-component>

   <child-component1 ngFor= let car of cars>
     <child-component2></child-component2>
   </child-component1>
</parent-component>

所以看起来像父组件,在子组件 1 中,我可以为子组件 2 定义一个占位符吗?

如:

child component 1

<child-component1 ngFor= let car of cars>
hier ist place holder

</child-component1>

如您所见,在使用 ngfor 的子组件中,我想将 car 放入子组件 2。 任何解决方案?

多谢

    标签: angular components ngrx


    【解决方案1】:

    使用ng-contenthttps://angular.io/guide/content-projection

    Parent component: 
    
    <parent-component>
    
       <child-component1>
         <child-component2></child-component2>
       </child-component1>
    </parent-component>
    
    
    child component 1
    
    <div>Some stuff here</div>
    <ng-content></ng-content> // <= this renders child-component2
    <div>Other stuff here</div>
    

    【讨论】:

    • 感谢您的回答,另一个问题,我可以给 ng-content 输入值吗?因为在子组件中使用 ngfor。我重新编辑了我的问题
    • 嗨,我已经编辑了我的问题,我的意思是,我可以使用 <ng-content [inputValue]=""></ng-content> 吗?
    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 2014-09-30
    • 2023-01-12
    • 2020-12-28
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多