Insert ng-content select dynamically

4.4k Views Asked by At

Simply put, I want to pass in the select value into the component for ng-content as an input. It should look somewhat like this:

parent:

<child-component [selectedValue]="['[selectedHtml1]','[selectedHtml2]']">
  <div selectedHtml1>I'm inside!!</div>
  <div selectedHtml2>I'm inside also!!</div>
</child-component>

child-html:

<ng-content *ngFor="let value of selectedValue" select="{{value}}">

child-ts:

@Input() selectedValue: string[];

but when I do this I get an error saying

compiler.js:2175 Uncaught Error: Template parse errors:
Can't bind to 'select' since it isn't a known property of 'ng-content'.
1. If 'select' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
    </mat-paginator>
  </div>
  <ng-content [ERROR ->]select="{{selectedValue}}"></ng-content>
</mat-card>
"): ng:///[path to my component]@[component line]
    at syntaxError (compiler.js:2175)
    at TemplateParser.parse (compiler.js:11388)
    at JitCompiler._parseTemplate (compiler.js:25961)
    at JitCompiler._compileTemplate (compiler.js:25949)
    at compiler.js:25893
    at Set.forEach (<anonymous>)
    at JitCompiler._compileComponents (compiler.js:25893)
    at compiler.js:25806
    at Object.then (compiler.js:2166)
    at JitCompiler._compileModuleAndComponents (compiler.js:25805)

Doing it with normal transclusion syntax works, so why doesn't this?

1

There are 1 best solutions below

6
AlleXyS On

In your html

<ng-content #select="{{selectedValue}}">

and in .ts

@ViewChild(select)