1

ngTemplateOutlet在我的 Angular 代码中实现时出现以下错误

compiler.js:2420 Uncaught Error: Template parse errors:
Parser Error: Missing expected } at column 47 in [searchListing; context: {$implicit: entityList:genericJobList,
canCreateEntity:canCreateJob, nextBookmarkList:genericNextBmJobList,'disableScroll:disableJobScroll}]
in ng:///SharedSearchModule/SearchCompareComponent.html@707:7 ("

它说错误在第 707 行。我在第 707 行的代码是ngTemplateOutlet自己的代码

<ng-container 
    *ngTemplateOutlet="searchListing; context: {$implicit: entityList:genericJobList,
    canCreateEntity:canCreateJob, nextBookmarkList:genericNextBmJobList, 
    disableScroll:disableJobScroll}">
</ng-container>

部分ng-template如下:

<ng-template #searchListing let-canCreateEntity="canCreateEntity" 
   let-entityList="entityList" let-nextBookmarkList="nextBookmarkList" 
   let-disableScroll="disableScroll">
   <!-- template code here -->
</ng-template>

}根据遇到的错误,我没有看到丢失。我传递的道具是布尔类型(canCreateEntity,disableScroll),对象数组(entityList)和字符串(nextBookmarkList)。我无法提供一个工作示例,因为我无法在不修改它的情况下共享完整代码。谁能指出错误是什么

4

1 回答 1

1

你的语法是错误的。对于 $implicit,您必须传递一个有效的 json 对象,因此您的上下文应如下所示:

{
  $implicit: {
    entityList: genericJobList,
    canCreateEntity: canCreateJob,
    nextBookmarkList: genericNextBmJobList,
    disableScroll: disableJobScroll
  }
}

当然,格式是可选的,只是为了让您的代码的差异更加清晰。您提供的错误消息甚至告诉您预期}应该在第 47 列中,这是第二个冒号所在的位置。这是该值不再被解释为有效 json 的点,因此它假设您忘记了右括号。

编辑

既然我仔细研究了您如何在 中使用上下文ng-template,您的上下文应该看起来像这样:

{
  entityList: genericJobList,
  canCreateEntity: canCreateJob,
  nextBookmarkList: genericNextBmJobList,
  disableScroll: disableJobScroll
}

如果你使用$implicit,你可以像这样引用上下文:

<ng-template let-whatever>
  {{ whatever }}
</ng-template>

由于您不使用该$implicit值,因此您不必提供一个。

于 2020-09-26T09:04:56.160 回答