Custom Angular Component Not Rendering

I am trying to create a custom reusable component based on material angular.

I have the following code dropdown.component.ts

import {AfterContentInit, Component, ContentChildren, QueryList, ViewChildren} from '@angular/core';
import {MatSelectModule} from "@angular/material/select";
import {MyOptionComponent} from "./dropdown-options.component";

@Component({
  selector: 'app-dropdown',
  standalone: true,
  imports: [
    MatSelectModule,
    MyOptionComponent
  ],
  template: `<mat-select><ng-content></ng-content></mat-select>`,
  styleUrl: './dropdown.component.scss'
})
export class DropdownComponent implements AfterContentInit {
  ngAfterContentInit(): void {
      console.warn('AfterContentInit', this.children);
  }
  @ContentChildren(MyOptionComponent) children!: QueryList<MyOptionComponent>;

}

dropdown
dropdown-options.component.ts

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

@Component({
  selector: 'my-option',
  standalone: true,
  template: `<option value="{{value}}">{{label}}</option>`, 
})
export class MyOptionComponent {
  @Input() label: string = ''; // Display text
  @Input() value: any;         // Value for the dropdown
}

then I try to use it with:

<app-dropdown>
  <my-option label="test" value="1" ></my-option>
  <my-option label="test" value="1" ></my-option>
  <my-option label="test" value="1" ></my-option>
</app-dropdown>

The console.warn('AfterContentInit', this.children); brings back the child elements, but they never show in the DOM

Anyone have any idea what I’m missing?

1

did you check console on possible errors? If there is no errors, other possible reason of not rendering is ChangeDetection strategy such as onPush, you need change it to onDefault or use markForCheck() method in a place when you need rendering

   import { Component, ChangeDetectorRef } from '@angular/core';
    
    @Component({
      selector: 'app-example',
      template: `
        <p>Count: {{ count }}</p>
        <button (click)="increment()">Increment</button>
      `,
      changeDetection: ChangeDetectionStrategy.OnPush 
    })
    export class ExampleComponent {
      count = 0;
    
      constructor(private cdr: ChangeDetectorRef) {}
    
      increment() {
        this.count++;
        // Manually trigger change detection
        this.cdr.markForCheck(); 
      }
    }

Check your imports if parent component(comoponent whose html you shared) has imported dropdown component in imports.
check the ts file of the parent component, look for missing import there, for this component, if everything looks fine but still not working, show me the parent component where you are using this dropdown ts and htlm also the routes file for the module the parent component is a part of.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật