簡體   English   中英

AngularJS 多槽嵌入

[英]AngularJS Multi-slot transclusion

我正在嘗試在 AngularJS 1.5.8 中使用多槽嵌入實現一個組件。

如果我使用指令,我的測試工作正常,但是對於組件,似乎連插槽都找不到! .

這就是我聲明組件的方式

app.component('asComponent', function() {
return {
  transclude: {
    'title': '?paneTitle',
    'body': 'paneBody',
    'footer': '?paneFooter'
  },
  template: `<h1>I am a component</h1>
              <div style="border: 2px solid red;">
              <div class="title" ng-transclude="title">Fallback Title</div>
              <div ng-transclude="body"></div>
              <div class="footer" ng-transclude="footer">Fallback Footer</div>
            </div>`
}});

app.controller('ExampleController', [ function() {
    var vm = this;
    vm.title = 'Lorem Ipsum';
    vm.link = 'https://google.com';
    vm.text = 'Neque porro quisquam est qui dolorem ipsum quia dolor...';
}]);

這里是 HTML

 <div ng-controller="ExampleController as $ctrl" class="container">
  <as-component>
      <pane-title>
        <a ng-href="{{$ctrl.link}}">{{title}}</a>
      </pane-title>
      <pane-body>
       <p>{{$ctrl.text}}</p>
      </pane-body>
   </as-component>
 </div>

官方 AngularJS 文檔說

在 AngularJS 中,Component 是一種特殊的指令,它使用更簡單的配置,適用於基於組件的應用程序結構。

如果是這種情況,那么多槽嵌入也應該與組件完美配合。

我知道我錯過了什么,但我看不到它是什么!

我用一個指令和一個組件創建了一個小的 Plunker。

https://plnkr.co/edit/yTMRD4SrH8CWLK4LQEwe?p=info

謝謝

對於組件,使用對象(不是函數):

app.component('asComponent', {
  transclude: {
    'title': '?paneTitle',
    'body': 'paneBody',
    'footer': '?paneFooter'
  },
  template: `<h1>I am a component</h1>
              <div style="border: 2px solid red;">
              <div class="title" ng-transclude="title">Fallback Title</div>
              <div ng-transclude="body"></div>
              <div class="footer" ng-transclude="footer">Fallback Footer</div>
            </div>`
});

另外,您在{{ title }}缺少$ctrl 它應該是:

<a ng-href="{{$ctrl.link}}">{{$ctrl.title}}</a>

這里它在plnkr 中工作。

我根本無法讓多插槽工作! 我很難過,嘗試了多個 1.8.2 的角度版本

輸出總是重復的。 命名插槽被忽略?

 <as-directive>
            <pane-title>Title</pane-title>
            <pane-body>Text</pane-body>
        </as-directive>

.directive('asDirective', [function () {
    return {
        restrict: 'E',
        transclude: {
            'title': '?paneTitle',
            'body': 'paneBody',
            'footer': '?paneFooter'
        },
        template: '<div style="border: 1px solid black;">' +
            '<div class= "title" ng-transclude="title">Fallback Title</div > '+
        '<div ng-transclude="body"></div>'+
                '<div class="footer" ng-transclude="footer">Fallback Footer</div>'+
            '</div>'
    }
}]);

輸出:

<div style="border: 1px solid black;"><div class="title" ng-transclude="title">
            <pane-title class="ng-scope">Title</pane-title>
            <pane-body class="ng-scope">Text</pane-body>
        </div> <div ng-transclude="body">
            <pane-title class="ng-scope">Title</pane-title>
            <pane-body class="ng-scope">Text</pane-body>
        </div><div class="footer" ng-transclude="footer">
            <pane-title class="ng-scope">Title</pane-title>
            <pane-body class="ng-scope">Text</pane-body>
        </div></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM