angular - 动态插入 ng-content 选择

标签 angular dynamic transclusion ng-content

简单地说,我想将 select 值作为输入传递到 ng-content 的组件中。它应该看起来像这样:

parent :

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

子 html:

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

child -ts:

@Input() selectedValue: string[];

但是当我这样做的时候我得到一个错误提示

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)

用普通的嵌入语法就可以了,那为什么不行呢?

最佳答案

在你的html中

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

在 .ts 中

@ViewChild(select)

关于angular - 动态插入 ng-content 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58998650/

相关文章:

jquery - 如何通过小部件代码使用网站的 jQuery

javascript - Angular 2 HTTP 服务不返回 promise

angular - UpperCasePipe ngModel 不适用于输入中的最后一个字符?

python - 使用 python 在 blender 中使用 addObject()

c# - 从 C# 动态类型调用 `Contains` 方法会产生错误 - 为什么?

c - 在c中输入动态结构数组的malloc函数中的元素数量

angularjs - 嵌入 (true) - 组合值

javascript - ng 嵌入为 : element vs attribute

javascript - Angular2 npm 包 - 我如何知道要下载什么?

javascript - 如何在 Angular 6/Ionic 4 中使用 fin-hypergrid 库