在命名的 transclude 槽中,我如何 * 只 * 嵌入元素的内容?

Nat*_*een 4 javascript angularjs angularjs-directive angularjs-ng-transclude

我有一个基本指令,我想使用嵌入。相关选项设置如下:

restrict: "E",
replace: true,
transclude: {
  'toolbar': 'toolbarSlot'
},
scope: {
  pageTitle: "@"
},
templateUrl: "path/to/my/template.html"
Run Code Online (Sandbox Code Playgroud)

我的模板是:

<header class="page-header">
  <h1 class="page-title heading">{{ pageTitle }}</h1>
  <div class="page-header-toolbar toolbar" ng-transclude="toolbar">
    <!-- "toolbar" transcluded content should go here -->
  </div>
</header>
Run Code Online (Sandbox Code Playgroud)

最后,当我使用指令时,我是这样使用它的:

<my-custom-page-header-directive page-title="Title">
  <toolbar-slot>
    <button>Button</button>
    <button>Another button</button>
  </toolbar-slot>
</my-custom-page-header-directive>
Run Code Online (Sandbox Code Playgroud)

问题是,在 DOM 中,它最终是这样的,一个无关的toolbar-slot元素混合到嵌入的内容中:

<header class="page-header">
  <h1 class="page-title heading">Title</h1>
  <div class="page-header-toolbar toolbar">
    <toolbar-slot>
      <button>Button</button>
      <button>Another button</button>
    </toolbar-slot>
  </div>
</header>
Run Code Online (Sandbox Code Playgroud)

有没有办法(使用ngTransclude)只嵌入插槽元素的内容

Nat*_*een 5

看起来答案是否定的,目前还没有办法做到这一点

Angular.js 存储库上的一个悬而未决的问题表明,对于实现以ngTransclude这种方式使用的能力存在疑虑:

我不喜欢向插槽嵌入添加替换功能。它会引入与普通“替换”和嵌入:元素相同的问题。

但是,显然,您“如果确实需要,实际上可以手动执行此操作”