简体   繁体   English

带有用于 child-dom 的插槽的 Angular 1.5 组件模板

[英]Angular 1.5 Component template with slot for child-dom

I'm attempting to make a reusable self contained angular 1.5 component that acts as a wrapper for other page content.我正在尝试制作一个可重用的自包含 angular 1.5 组件,作为其他页面内容的包装器。 When a value is changed it will toggle the child content.当一个值改变时,它会切换子内容。 The solution of hiding and showing content is not in question but how to make a reusable component that can handle injecting the children into the components template and not over write the template.隐藏和显示内容的解决方案不是问题,而是如何制作一个可重用的组件,可以处理将子项注入组件模板而不是覆盖模板。

page.html页面.html

<div class="some page">
   <my-cmp value="false">
      <p>this static content is toggled by my-cmp</p>
      <div>more stuff</div>
      <ul>
         <li>no limits</li>
      </ul>
   </my-cmp>
</div>

When value is changed the component should change the content on the page.当值改变时,组件应该改变页面上的内容。

<div class="some page">
   <my-cmp value="true">
      <div>
         <h1>Content is hidden</h1>
         <button>Show content</button>
      </div>
   </my-cmp>
</div>

I have been reading the angular 1.5 docs and haven't found any example that does this.我一直在阅读 angular 1.5 文档,但没有找到任何这样做的例子。 The problem i'm having is that the child content is over writing the content in my components template and i have no way of telling the component the exact spot to place the child content.我遇到的问题是子内容在我的组件模板中覆盖了内容,我无法告诉组件放置子内容的确切位置。 The component should be able to switch between the two states freely.组件应该能够在两种状态之间自由切换。

my-cmp.html我的-cmp.html

<div class="my-cmp">
   <div ng-if="showContent">
      <!--child content should be inserted here by the component -->
      <!-- how do i tell the my-cmp template i want child elements injected here? -->
      <!-- this is the only part i'm missing -->
   </div>
   <div ng-if="!showContent">
      <h1>Content is hidden</h1>
      <button>Show content</button>
   </div>
</div>  

I managed to find some old documents showing how to do this.我设法找到了一些显示如何执行此操作的旧文档。 the transclude: true, in the component is needed. transclude: true,在组件中是需要的。

my-cmp.js我的 cmp.js

(function() {
    angular.module("app").component("my-cmp", {
        templateUrl: "views/tmpl/components/my-cmp.html",
        controller: [myCmp],
        restrict: 'E',
        transclude: true,
        binding: {
            show: '<',
        }
    });

    function myCmp() {...}
})();

my-cmp.html我的-cmp.html

<div class="my-cmp">
    <div class="content" ng-if="showContent" ng-transclude>
        <!-- content is added by children passed in -->
    </div>
    <div ng-if="!hideContent">
       <h1>Content is hidden</h1>
       <button>Show content</button>
    </div>
</div>

adding ng-transclude as an attribute to the element that is the target, will tell angular to inject the child DOM elements in this spot and not over write your template.ng-transclude作为属性添加到作为目标的元素,将告诉 angular 在此位置注入子 DOM 元素,而不是覆盖您的模板。

(function() {
    angular
    .module('app.my-component')
    .component('myCmp', {
        transclude: true,
        bindings: {
            showContent: '='
        },
        templateUrl: 'my-cmp.html',
        controller: 'MyCmpController'
    });
})();

(function() {
    angular
    .module('app.my-component')
    .controller('MyCmpController', MyCmpController);

    /*@ngInject*/
    function MyCmpController($scope) {
        $scope.showHiddenContent = showHiddenContent;

        function showHiddenContent() {
            $scope.showContent = !$scope.showContent;
        }
    }
})();

In HTML:在 HTML 中:

<div class="some-page">
    <my-cmp show-content="isHidden">
        <p>this static content is toggled by my-cmp</p>
        <div>more stuff</div>
        <ul>
            <li>no limits</li>
        </ul>
    </my-cmp>
</div>

my-cmp.html我的-cmp.html

<div class="my-cmp">
    <div class="content" ng-if="showContent" ng-transclude> <!-- ng-show="showContent" -->
        <!-- content is added by children passed in -->
    </div>
    <div ng-if="!showContent"> <!-- ng-hide="!showContent" -->
        <h1>Content is hidden</h1>
        <button type="button" ng-click="showHiddenContent()">Show content</button>
    </div>
</div>

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

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