简体   繁体   English

Angular - 在带有子自定义组件的外部 div 上使用行类

[英]Angular - Use row class on outside div with child custom components

I have a customized component with bootstrap columns as input parameters that works like this:我有一个带有引导列作为输入参数的自定义组件,其工作方式如下:

<div *ngIf="isColumn" [ngClass]="'col-md-' + labelColumns">
    <label>
        {{label}}
        <i *ngIf="isRequired" class="required-asterisk">*</i>:
    </label>
</div>

<div *ngIf="isColumn" [ngClass]="'col-md-' + inputColumns">
    <input [attr.maxlength]="maxLength" [(ngModel)]="value" [eds-required]="isRequired" />
</div>

I want to control the row class from outside this template so i can create multiple inline components like this one.我想从这个模板外部控制行类,这样我就可以创建多个像这样的内联组件。 So i did this:所以我这样做了:

<div class="row no-gutters">
    <quick-text-input isColumn="true" isRequired="true" label="TESTE"></quick-text-input>
</div>

However, the text input components are appearing above each other, as if the template didn't recognize the outside div's row class.但是,文本输入组件出现在彼此之上,就好像模板没有识别外部 div 的行类一样。

Is there a way for bootstrap div rows to align custom templates with columns?有没有办法让引导 div 行将自定义模板与列对齐?

Edit: labelColumns and inputColumns are two input variables with default values编辑:labelColumns 和 inputColumns 是两个具有默认值的输入变量

In Bootstrap 3, the CSS used for the row class was display: table .在 Bootstrap 3 中,用于row类的 CSS 是display: table The code you have written would work if used with Bootstrap 3 (along with the right Bootstrap 3 class names).如果与 Bootstrap 3(以及正确的 Bootstrap 3 类名)一起使用,您编写的代码将起作用。 Bootstrap 4, on the other hand, is built with flexbox , so the row class now uses display: flex instead.另一方面,Bootstrap 4 是用flexbox 构建的,所以row类现在使用display: flex代替。 When display: flex; display: flex; is used, it only applies to its direct children.使用,它只适用于它的直接孩子。 In your code, the direct children of the container that uses row contain quick-text-input and the first element within quick-text-input contains the col-md- classes, so your grid will break since the contents of quick-text-input are not direct children of the row container.在代码中,容器直接儿童使用row包含quick-text-input和内第一要素quick-text-input包含col-md-类,所以您的电网将因为内容突破quick-text-input不是row容器的直接子级。

From the docs :文档

To create a flex container, we set the value of the area's container's display property to flex or inline-flex .要创建 flex 容器,我们将区域容器的display属性的值设置为flexinline-flex As soon as we do this the direct children of that container become flex items.一旦我们这样做,该容器的直接子项就会成为flex项目。

What you need is basically a nested flexbox where the inner flexbox would be quick-text-input and the outer one would be the div in your parent component which encapsulates the quick-text-input elements.您需要的基本上是一个嵌套的 flexbox,其中内部 flexbox 将是quick-text-input ,而外部 flexbox 将是您的父组件中的div ,它封装了quick-text-input元素。

quick-text-input.component.html快速文本输入.component.html

<div class="row">
    <div *ngIf="isColumn" class="col-4">
        <label>
            {{label}}<i *ngIf="isRequired" class="required-asterisk">*</i> :
        </label>
    </div>

    <div *ngIf="isColumn" class="col-8">
        <input class="form-control" [attr.maxlength]="maxLength" [(ngModel)]="value" />
    </div>
</div>

You don't seem to be passing labelColumns and inputColumns as @Input so I have just changed them to col-md-4 and col-md-8 respectively.您似乎没有将labelColumnsinputColumns作为@Input传递,所以我刚刚将它们分别更改为col-md-4col-md-8 You can used labelColumns and inputColumns if you need them.如果需要,您可以使用labelColumnsinputColumns

Now, in the parent component, depending on how many elements we need in a row, we need to set the col- class in our quick-text-input element.现在,在父组件中,根据一行中需要多少个元素,我们需要在quick-text-input元素中设置col- quick-text-input class。

<div class="row">
    <span class="mb-2 mx-3">Single element in row</span>
    <quick-text-input class="col-12" isColumn="true" isRequired="true" label="TESTE"></quick-text-input>

    <span class="w-100 mb-2 mx-3">Two elements in a row</span>
    <quick-text-input class="col-6" isColumn="true" isRequired="true" label="TESTE"></quick-text-input>
    <quick-text-input class="col-6" isColumn="true" isRequired="true" label="TESTE"></quick-text-input>
</div>

Here is an example on StackBlitz .这是一个关于StackBlitz的例子。 This is how the output will look.这就是输出的外观。

You could move the row class of bootstrap directly to the component tag and also if you're using bootstrap 4, you need to use col-3 instead of col-md-3 , below is a working example, please let me know if this fixes your issue!您可以将引导程序的row类直接移动到组件标记,并且如果您使用引导程序 4,则需要使用col-3而不是col-md-3 ,下面是一个工作示例,请让我知道这是否解决您的问题!

// app.component.html
<app-test class="row no-gutters" isColumn="true" isRequired="true" label="TESTE"></app-test>

// test.component.html
<div *ngIf="isColumn" class="col-{{ labelColumns }}">
    <label>{{label}}<i *ngIf="isRequired" class="required-asterisk">*</i>:</label>
</div>

<div *ngIf="isColumn" class="col-{{ inputColumns }}">
    <input [attr.maxlength]="maxLength" [(ngModel)]="value" />
</div>

StackBlitz 闪电战

you are right , this sholud work like in this example:你是对的,这个 sholud 就像在这个例子中一样:

https://stackblitz.com/edit/angular-4qn1de https://stackblitz.com/edit/angular-4qn1de

Even if your "labelColumns" is undefined, the input-field are aligned in a row.即使您的“labelColumns”未定义,输入字段也会在一行中对齐。

But , if i'm removing the boostrap-import from style.css, then the input-fields are align amonng themselves.但是,如果我要从 style.css 中删除 boostrap-import,那么输入字段就会自己对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM