The CSS styling from the Angular parent form-control Bootstrap is not being inherited by the child component

I have developed an autocomplete child component that I am integrating into a parent component. However, when I try to apply Bootstrap form-control validation in the parent component, it does not seem to affect this child component - which is essentially an input box with a dropdown list. Strangely, the validation works perfectly fine on other controls that are not child components.

Child component HTML:

<div class="searching">
  <input type="text" class="form-control" (input)="getFilteredData(inputBox.value);" [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:

.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;
  font-family: Arial,Helvetica,sans-serif;
  font-size: 1em;
  text-align: left;
}

Answer №1

In my toolbox, I have a few solutions that might be able to help you out.

One option is to add the property 'encapsulation' with ViewEncapsulation.None in your parent component decorator, but be cautious as this can cause styles from the parent component to affect the entire application. Make sure to use a very specific parent selector to minimize any unintended consequences.

Another suggestion is to use @Input property on the child component to pass a validation boolean and apply the appropriate class based on the input value.

You could also create a .parent-selector .is-invalid selector in the parent component and then use ::ng-deep .parent-selector .is-invalid to style child elements. Keep in mind that this method is deprecated and may not work in future updates.

Remember: when adding the is-invalid class to a selector, keep in mind that it might target a different element than intended. It's often best to utilize inputs for more precise control over styling.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mocked observables are returned when testing an Angular service that includes parameters

I'm currently exploring various types of unit testing and find myself struggling with a test for a service once again. Here is the function in my service that I need to test: Just to clarify: this.setParams returns an object like {name: 'Test&ap ...

Is there a way to apply CSS styles to a specific word within a div that corresponds to the word entered in the search box?

Using a searchbox, you can enter words to find pages with the searched-for word in the page description. If the word is found in the description, a link to the page along with the highlighted word will be displayed. However, I am encountering an issue wher ...

The CSS3 transition is not functioning correctly when transitioning element dimensions from a percentage or auto value to a specific pixel

As part of my project, I am developing a single-page website that features a vector graphic occupying 80% of the screen width on the initial 'start screen'. Once the user scrolls down, the graphic smoothly transitions into a navigation bar positi ...

Include a return or delete from the default IO statement

I am utilizing an intersection observer that alters the header's font-color and background-color based on the content intersecting with it. This change is determined by the presence of data-color and data-background attributes declared on the div/sect ...

Filtering out specific properties from an element within a JavaScript forEach loop

const findBloodType = bloodCodeArray.find(bloodCode => bloodCode.code.toUpperCase() === bloodType.toUpperCase()).code; In my Angular code, I am trying to retrieve just the 'code' property of the 'bloodCode' element using a callback ...

What is the best way to customize CSS in Material UI?

When working with material UI and reactjs, I encountered an issue while trying to override the button color without affecting the tab colors (see screenshot). How can I achieve this using themes in material UI? Code: const theme = createMuiTheme({ p ...

What is the best way to link to this list of options?

#episode-list { padding: 1em; margin: 1em auto; border-top: 5px solid #69c773; box-shadow: 0 2px 10px rgba(0,0,0,.8) } input { width: 100%; padding: .5em; font-size: 1.2em; border-radius: 3px; border: 1px solid #d9d9d9 } <div id="epis ...

The astonishing font-icon animation feature is exclusively functional on code-pen

I have a problem with a font-icon animation code. When I run it on my local server, the animation doesn't work. It only works on http://codepen.io/TimPietrusky/pen/ELuiG I even tried running it on http://jsfiddle.net/qjo7cf3j/ @import url(http: ...

How to generate a new array in Angular by combining elements from two existing arrays for common items

I am currently working on a TypeScript function to compare two arrays and generate a third array containing the common items. For example: employees: any; offices: any; constructor() { this.employees = [ { fname: "John", lname: "James", sta ...

Load Angular template dynamically within the Component decorator

I am interested in dynamically loading an angular template, and this is what I have so far: import { getHTMLTemplate } from './util'; const dynamicTemplate = getHTMLTemplate(); @Component({ selector: 'app-button', // templat ...

Tips for resizing all HTML elements when adjusting browser window dimensions

I am working with the following HTML code snippet: <div id="sectionOne" class="container-fluid d-flex flex-column justify-content-center align-items-center" style="height: 80vh;"> <div class="row d-flex justify-content-center" style="color: #00 ...

Tips for dynamically adding an object to the formArrayName in Angular reactive forms

Currently, I am facing an issue with loading my array of objects response into a multiselect using ng-select. The structure of my response data looks like this: The formModel setup in my code is as follows: clientForm = this.fb.group({ custom : this. ...

Understanding the Intrinsic Dimensions of Replaced Elements in CSS

I'm currently facing some challenges with the CSS Intrinsic & Extrinsic Sizing Module Level 3, specifically chapter 4.1 on Intrinsic Sizes. This section is proving to be quite difficult for me: When a block-level or inline-level replaced element h ...

Is it possible to modify the HTML/CSS of a child component using the parent component in Angular 2+?

Is there a way to dynamically add text and style to a specific row in a child component based on the parent's logic? (parent): <body> <child-component></child-component> </body> (child): <div *ngfor = "let record in r ...

Setting a data type for information retrieved from an Angular HTTP request - A Step-by-Step Guide

Here is the code I use to fetch data: login() { const url = api + 'login'; this.http.post(url, this.userModel) .subscribe( data => { localStorage.token = data.token; this.router.navigate(['/home&a ...

Increase the visibility of a div using Jquery and decrease its visibility with the "

Can anyone assist me with implementing a "show more" and "show less" feature for a specific div? I have put together a DEMO on codepen.io In the DEMO, there are 8 red-framed div elements. I am looking to display a "show more" link if the total number of ...

Navigating on Angular 2 without refreshing the page

Seeking advice for my Angular 2 single-page application project which heavily uses maps. The main requirement is that when a point on the map is clicked, the URL should update so that upon refresh, the map zooms into that specific point (and also to allow ...

What is the process for changing the color of material-icons to white in an Angular Dart project using angular_components?

Is there a way to set the color of material-drawer material-icon to white on a dark background in an angular_components project using Angular Dart? app_component.html <material-drawer persistent #drawer="drawer" [class.custom-width]= ...

Having some trouble getting the text to float alongside an image properly in HTML and CSS

I'm having trouble positioning my text next to an image. I've tried floating it to the right and even using a flexbox, but nothing seems to be working well. Here is the code I have... <div class="tab-content"> <div clas ...

Is there a way to implement an extra placeholder attribute with Angular4?

Currently, in a project where I am utilizing Angular Material, there is a form integrated with an autocomplete component. The functionality works as expected but I am interested in implementing placeholder text once the user focuses on the input element. ...