简体   繁体   English

angular2 ng-container 如何使用动态 ngTemplateOutletContext

[英]angular2 ng-container how to use dynamic ngTemplateOutletContext

I'm trying to use ng-container with NgTemplateOutlet ( https://angular.io/docs/ts/latest/api/common/index/NgTemplateOutlet-directive.html )我正在尝试将 ng-container 与 NgTemplateOutlet ( https://angular.io/docs/ts/latest/api/common/index/NgTemplateOutlet-directive.html )一起使用

      <div *ngFor="let child of children; let i=index">
      <ng-container *ngTemplateOutlet="inputTpl; { $implicit: child, index: i }"></ng-container>
  </div>

Thie results in the error Thie导致错误

Unexpected token {, expected identifier, keyword, or string at column 11 in [inputTpl; [inputTpl; 中第 11 列的意外标记 {、预期标识符、关键字或字符串; { $implicit: child, index: i }] { $implicit: child, index: i }]

When I use 'context:' like in the docs, this leads to当我在文档中使用“上下文:”时,这会导致

Can't bind to 'ngTemplateOutletContext' since it isn't a known property of 'ng-container'无法绑定到“ngTemplateOutletContext”,因为它不是“ng-container”的已知属性

If I use a object declared in my ts file and set it instead of my Object, everything is working fine.如果我使用在我的 ts 文件中声明的对象并设置它而不是我的对象,则一切正常。 Moreover this is also working fine:此外,这也工作正常:

      <div *ngFor="let child of children; let i=index" class="form-group">
      <template [ngTemplateOutlet]="inputTpl" [ngOutletContext]="{ $implicit: child, index: i }" />
    </div>

Does anybody know, how I can use ng-container with *ngTemplateOutlet and ngTemplateOutletContext generated in the html?有谁知道,我如何将 ng-container 与在 html 中生成的 *ngTemplateOutlet 和 ngTemplateOutletContext 一起使用?

Did you try <ng-container> with [ngTemplateOutletContext] like this?你有没有像这样用[ngTemplateOutletContext]尝试<ng-container>

<ng-container
    [ngTemplateOutlet]="inputTpl"
    [ngTemplateOutletContext]="{ $implicit: child, index: i }"
>
</ng-container>

Example for angular 5.角度 5 的示例。

<ng-container  [ngTemplateOutlet]="optionTemplate.template" [ngTemplateOutletContext]="{option:option}">
</ng-container>


<p-auto-complete property='searchText' [options]="options"(onChange)="select($event)">
        <ng-template pOptionTemplate  let-option="option">
            <div>
              //...
            </div> 
        </ng-template>
 </p-auto-complete>

stackblitz闪电战

Maybe you should update your dependencies to 2.4?也许您应该将依赖项更新到 2.4?

  "dependencies": {
    "@angular/common": "~2.4.3",
    "@angular/compiler": "~2.4.3",
    "@angular/core": "~2.4.3",
    "@angular/forms": "~2.4.3",
    "@angular/http": "~2.4.3",
    "@angular/platform-browser": "~2.4.3",
    "@angular/platform-browser-dynamic": "~2.4.3",
    "@angular/platform-server": "~2.4.3",
    "@angular/router": "~3.4.3",
    "@angularclass/conventions-loader": "^1.0.2",
    "@angularclass/hmr": "~1.2.2",
    "@angularclass/hmr-loader": "~3.0.2",
    "core-js": "^2.4.1",
    "http-server": "^0.9.0",
    "ie-shim": "^0.1.0",
    "jasmine-core": "^2.5.2",
    "reflect-metadata": "^0.1.9",
    "rxjs": "~5.0.2",
    "zone.js": "~0.7.4"
  },

call to the main template调用主模板

<ng-template *ngTemplateOutlet="callMain; context: { $implicit: item }"></ng-template> // can use multiple places

main template主模板

<ng-template #callMain let-item>
  //use dynamic item object
</ng-template>

您可能必须在声明当前组件的模块中导入CommonModule

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

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