【问题标题】:Bootstrap Grid System not working in Angular Components communicating from parent to childBootstrap Grid System 无法在 Angular 组件中从父级到子级进行通信
【发布时间】:2023-03-31 07:53:01
【问题描述】:

app.component.html 代码

<div class="container">
    <div class="row" id="ads">
        <div class="col-xs-4">
        <app-card  *ngFor="let car of cars"
        [carNotifyBadge]="car.carNotifyBadge"
        [carNotifyYear]="car.carNotifyYear"
        [carCondition]="car.carCondition"
        [carPrice]="car.carPrice"
        [carUsageinKM]="car.carUsageinKM"
        [carName]="car.carName"
        [imgSource]="car.imgSource"
        >
        </app-card>

    </div>
    </div> 
    </div>

app.component.ts 代码

                             import { Component ,Input} from '@angular/core';

                         @Component({
                          selector: 'app-root',
                          templateUrl: './app.component.html',
                          styleUrls: ['./app.component.css']      
                         })
                            export class AppComponent {

                            title:string = "Angular-App"

                            cars = [

                                {....},{....},{....}

                    ]


                    }

card.component.html

                     <div class="card rounded">
                          <div class="card-image">
                              <span class="card-notify-badge">{{carNotifyBadge}}</span>
                              <span class="card-notify-year">{{carNotifyYear}}</span>
                              <img class="img-fluid" [src]="imgSource" alt="Alternate Text" />
                          </div>
                          <div class="card-image-overlay m-auto">
                              <span class="card-detail-badge">{{carCondition}}</span>
                              <span class="card-detail-badge">{{carPrice}}</span>
                              <span class="card-detail-badge">{{carUsageinKM}}</span>
                          </div>
                          <div class="card-body text-center">
                              <div class="ad-title m-auto">
                                  <h5>{{carName}}</h5>
                              </div>
                              <a class="ad-btn" href="#">View</a>
                          </div>
                      </div>

card.component.ts

                import { Component, OnInit, Input } from '@angular/core';

                @Component({
                  selector: 'app-card',
                  templateUrl: './card.component.html',
                  styleUrls: ['./card.component.css']
                })
                export class CardComponent implements OnInit {

                  constructor() { }

                  @Input()  carNotifyBadge = '';
                  @Input()  carUsageinKM = '';
                  @Input()  carName = '';
                  @Input()  carNoticarNotifyYearfyBadge = '';
                  @Input()  imgSource = '';
                  @Input()  carCondition = '';
                  @Input()  carPrice = '';
                  @Input()  carNotifyYear = '';

                  ngOnInit(): void {
                  }

                }

自从在 app.component.html 中,我使用了引导程序的网格系统并使用结构指令 ngFor,我正在向 DOM 动态添加元素。我希望这些列是并排的,而我将它放在彼此下方,如图所示。

如何根据引导行为并排显示列?

【问题讨论】:

  • *ngFor="let car of cars"col-xs-4 的内部元素,因此您可能希望您的 car 具有 col-xs-4 类,而不是父元素
  • @MrT 因为 *ngFor 指令将使用 selecor 动态添加 card.component.html 内容。因此,我希望整个选择器能够获得引导类 col-xs-4,因此 选择器位于类“col-xs-4”的 div 上方。能否请您详细说明您的思考过程?
  • 您有 1 个 div,即 col-xs-4,包含多辆汽车。所以包含所有汽车的元素不能超过屏幕的 1/3。您真正想做的是每辆车都有col-xs-4 类。所以只需将类放在app-card 标签上,而不是父div。您可以查看 Chrome 的调试器并查看框的大小,您可能会更好地了解我们的意思。

标签: html css angular bootstrap-4


【解决方案1】:

确保引导 CSS 已加载到应用程序中,如果没有,请按照以下步骤操作。 Steps to include Bootstrap in Application

仅将汽车项目传递给子组件,汽车对象 yu 中的所有属性都可以在子组件中访问。

<div class="container">
    <div class="row" id="ads">
        <div class="col-xs-6 col-md-4" *ngFor="let car of cars">
          <app-card [car]="car"></app-card>
      </div>
    </div> 
</div>

在子组件中,接收来自父组件的汽车对象输入。 @输入汽车;

【讨论】:

  • 只传递 [car] 对象是一个很好的建议。在卡片组件模板中,我需要单独引用每个键,例如 {{car.carName}}。这是正确的吗?
【解决方案2】:

确保该引导程序包含在您的 bundled.css 文件中。

是否有“#ads”任何覆盖引导行为的样式?

安装引导程序的有用链接using-bootstrap-with-angular

【讨论】:

  • id #ads 已保留以供 div 将来参考,并且与引导库无关。我什至可以删除 id。您认为#ads id 是如何覆盖默认引导行为的?
  • 必须在你的全局css或组件css中定义一些样式属性引用它吗?如果您检查#ads 元素,是否有任何来自引导程序的样式定义?
  • 典型的 .row 看起来像 ``` .row { display: -ms-flexbox;显示:弯曲; -ms-flex-wrap:换行;弹性包装:换行;边距右:-15px;左边距:-15px; }```
【解决方案3】:

我有类似的问题。我使用引导程序和基于它的商业主题。我试图在引导网格中显示记录列表。

我有显示网页的主要应用程序组件。然后我有卡片列表组件,它在应用组件中显示网格列表。

当我输入时:

  styleUrls: [
'./credential-card.component.css',
'../../assets/css/bootstrap.min.css',
'../../assets/css/style.css']

卡片样式正确,但网格不起作用。 当我删除样式时

  styleUrls: [
'./credential-card.component.css']

我失去了样式,但网格工作正常。

解决问题的方法是实现另一个组件 card-detail 并从 card-list 中调用它。我从 card-list 中删除了 bootstrap CSS,并添加到 card-detail 中。

最后,我在卡片列表组件中有这样的东西:

<section class="pricing-wrapper pt-70 pb-100">
<div class="container">
    <div class="row justify-content-center">
        <div *ngFor="let item of credentials" class="col-lg-4 col-md-6 col-sm-8">
            <card-detail [shortName]="item.payload.doc.data().shortName" [priceDescription]="item.payload.doc.data().priceDescription"></card-detail>
        </div>
    </div>
</div>

这是卡片详细信息:

<div class="pricing-style-5 mt-30">
    <div class="pricing-header">
        <div class="pricing d-flex align-items-center flex-wrap">
            <h3 class="heading-3 font-weight-400 title">{{shortName}}</h3>
            <span class="price">{{priceDescription}}</span>
        </div>
    </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-23
    • 2018-08-12
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2016-09-18
    • 2020-06-22
    相关资源
    最近更新 更多