简体   繁体   English

角父窗体控件Bootstrap / CSS不适用于子组件

[英]Angular parent form-control Bootstrap / CSS is not applied to child component

I created a autocomplete child component that I am using in parent. 我创建了一个在父级中使用的自动完成子级组件。 In parent when try to apply the validation using Bootstrap form-control it's not getting applied to this child component - which is an input box with a list, although it's getting applied to other controls which are not child. 在父级中,当尝试使用Bootstrap表单控件应用验证时,它不会应用到此子组件-该子组件是带有列表的输入框,尽管它正在应用到不是子组件的其他控件。

Child component HTML: 子组件HTML:

<div class="searching">
  <input type="text" class="form-control"  (input)="getFilteredData(inputBox.value);" class="form-control"  [formControl]="inputBox">
  <div id="search" tabindex="0" >
    <ul class="suggestionList">
      <li  *ngFor="let result of filteredResults | async" (click)="onUserSelected(result)" >{{result[displayField1]}} | {{result[displayField2]}} {{result[displayField3]}}</li>
    </ul>
  </div>
</div>

Parent component: 父组件:

<app-auto-complete formControlName="requestorId" 
                     [ngClass]="{ 'is-invalid': submitted && 
                     requestorId.errors }"></app-auto-complete>
                  <div *ngIf="submitted && f.requestorId.errors" class="invalid-feedback">
                      <div *ngIf="f.requestorId.errors.required">Requestor ID is required</div>
                  </div>

Child CSS: 子CSS:

.searching {
  width: inherit;
    position: relative;
}
      .searching input {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 30px;
      }
      .suggestionList {
        background-color: #fff;
        width: 100%;
        position: absolute;
        padding: 0;
        left: 0;
        z-index: 1000;
      }
      .suggestionList li {
        list-style-type: none;
        border: 1px solid #c5c5c5;
        cursor: pointer;
        left: 0;
        font-family: Arial,Helvetica,sans-serif;
        font-size: 1em;
        text-align: left;
      }

I have several workaraounds in mind, so I hope any helps. 我考虑了几个工作原理,因此希望对您有所帮助。

As mentioned in the comments, add in your parent component decorator the property 'encapsulation' (same level as 'selector' and such) with ViewEncapsulation.None. 如评论中所述,在您的父组件装饰器中添加带有ViewEncapsulation.None的属性“ encapsulation”(与“ selector”相同)。 Be aware that this approach makes styles of the parent component penetrate the whole app, so be wary with doing this unless you have the parent selector be really specific such as .my-specific-parent-selector-which-isnt-gonna-be-repeated 请注意,这种方法会使父组件的样式渗透到整个应用程序中,因此请谨慎行事,除非您让父选择器非常具体,例如.my-specific-parent-selector-which-isnt-gonna-be-repeated

use @Input property on child component to pass the validation boolean, and apply the class directly according to the Input passed. 在子组件上使用@Input属性传递验证布尔值,并根据传递的Input直接应用该类。

add a .parent-selector .is-invalid selector to parent, and then add behind it a ::ng-deep .parent-selector .is-invalid making that specific styling penetrate child classes. 向父级添加.parent-selector .is-invalid选择器,然后在其后添加::ng-deep .parent-selector .is-invalid以使特定样式渗透子类。 Be wary that this approach is deprecated though, so it might stop working in the future (although unlikely) 请注意,此方法已过时,因此将来可能会停止工作(尽管不太可能)

Note: also note that you are applying the is-invalid class to a selector.. if you inspect with chrome web browser you will see that this selector usually is a different element that where you try to add your class... so maybe your best approach is using inputs 注意:还请注意,您正在将is-invalid类应用于选择器..如果您使用chrome网络浏览器进行检查,您会发现该选择器通常是一个与尝试添加类不同的元素...所以也许最好的方法是使用输入

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

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