• <i id='MuEZB'><tr id='MuEZB'><dt id='MuEZB'><q id='MuEZB'><span id='MuEZB'><b id='MuEZB'><form id='MuEZB'><ins id='MuEZB'></ins><ul id='MuEZB'></ul><sub id='MuEZB'></sub></form><legend id='MuEZB'></legend><bdo id='MuEZB'><pre id='MuEZB'><center id='MuEZB'></center></pre></bdo></b><th id='MuEZB'></th></span></q></dt></tr></i><div id='MuEZB'><tfoot id='MuEZB'></tfoot><dl id='MuEZB'><fieldset id='MuEZB'></fieldset></dl></div>

      <small id='MuEZB'></small><noframes id='MuEZB'>

      1. <legend id='MuEZB'><style id='MuEZB'><dir id='MuEZB'><q id='MuEZB'></q></dir></style></legend>

        <tfoot id='MuEZB'></tfoot>
          <bdo id='MuEZB'></bdo><ul id='MuEZB'></ul>

      2. Angular 2 将组件动态附加到 DOM 或模板

        Angular 2 append Component dynamic to DOM or template(Angular 2 将组件动态附加到 DOM 或模板)

          <legend id='AuV0W'><style id='AuV0W'><dir id='AuV0W'><q id='AuV0W'></q></dir></style></legend>
            <tbody id='AuV0W'></tbody>

        • <small id='AuV0W'></small><noframes id='AuV0W'>

          <tfoot id='AuV0W'></tfoot>
          1. <i id='AuV0W'><tr id='AuV0W'><dt id='AuV0W'><q id='AuV0W'><span id='AuV0W'><b id='AuV0W'><form id='AuV0W'><ins id='AuV0W'></ins><ul id='AuV0W'></ul><sub id='AuV0W'></sub></form><legend id='AuV0W'></legend><bdo id='AuV0W'><pre id='AuV0W'><center id='AuV0W'></center></pre></bdo></b><th id='AuV0W'></th></span></q></dt></tr></i><div id='AuV0W'><tfoot id='AuV0W'></tfoot><dl id='AuV0W'><fieldset id='AuV0W'></fieldset></dl></div>
            • <bdo id='AuV0W'></bdo><ul id='AuV0W'></ul>

                  本文介绍了Angular 2 将组件动态附加到 DOM 或模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果调用 show(),我计划向 DOM 添加一个动态组件.我知道有一个使用 ngIf 或 [hidden] 的解决方案来隐藏它并将其用作指令,但我不喜欢这个解决方案,因为我不想在我的 HTML 中声明它.

                  I'm planning to add a component dynamic to the DOM if show() is called. I know there is a solution with ngIf or [hidden] to hide it and use it as a directive, but I'm not a fan of this solution because I don't want to declare it in my HTML.

                    import {Component} from 'angular2/core';
                    import {InfoData} from '../../model/InfoData';
                  
                    @Component({
                      selector: 'Info',
                      templateUrl: './components/pipes&parts/info.html',
                      styleUrls: ['./components/pipes&parts/info.css']
                    })
                  
                    export class Info{
                      infoData: InfoData;
                  
                      public show(infoData: InfoData) {
                        this.infoData= infoData;
                        document.body.appendChild(elemDiv); <----- Here?
                      }
                    }
                  

                  然后我将其声明为提供程序,以便我可以调用 show().

                  and then I declare this as a Provider so i can call show().

                    import {Component} from 'angular2/core';
                    import {Info} from './components/pipes&parts/Info';
                  
                    @Component({
                      selector: 'Admin',
                      templateUrl: './Admin.html',
                      styleUrls: ['./Admin.css'],
                      directives: [Info],
                      providers: [Info]
                    })
                  
                    export class Admin {
                      constructor(private info: Info) {
                      info.show(); <---- append the Info Element to DOM
                    }
                  

                  推荐答案

                  我认为您不需要将 Info 组件作为提供者提供给其他组件.我不确定它是否有效.您可以利用 QueryQueryView 来引用另一个使用的组件:

                  I don't think that you need to provide the Info component as providers to the other component. I'm not sure that it even works. You can leverage Query and QueryView to reference components used in another one:

                  @Component({
                    selector: 'Admin',
                    templateUrl: './Admin.html',
                    styleUrls: ['./Admin.css'],
                    directives: [Info]
                  })
                  export class Admin{
                    constructor(private @Query(Info) info: QueryList<Info>) {
                      info.first().show(); <---- append the Info Element to DOM
                    }
                  }
                  

                  您可以按照 Günter 的建议,使用 DynamicComponentLoader 动态添加此组件,而不是在 Info 组件中添加元素:

                  Instead of adding the element within the Info component, you can dynamically add this component using the DynamicComponentLoader as suggested by Günter:

                  @Component({
                    selector: 'Info',
                    templateUrl: './components/pipes&parts/info.html',
                    styleUrls: ['./components/pipes&parts/info.css']
                  })
                  
                  export class Info{
                        infoData: InfoData;
                  
                    public show(infoData: InfoData) {
                      this.infoData= infoData;
                      // No need to add the element dynamically
                      // It's now part of the component template
                      // document.body.appendChild(elemDiv); <----- Here?
                    }
                  }
                  
                  @Component({
                    selector: 'Admin',
                    //templateUrl: './Admin.html',
                    // To show where the info element will be added
                    template: `
                      <div #dynamicChild>
                        <!-- Info component will be added here -->
                      </div>
                    `,
                    styleUrls: ['./Admin.css'],
                    directives: [Info]
                  })
                  export class Admin{
                    constructor(private dcl: DynamicComponentLoader, private eltRef:ElementRef) {
                      this._dcl.loadIntoLocation(Info, this._el, 'dynamicChild')
                          .then(function(el) {
                            // Instance of the newly added component
                          });
                    }
                  }
                  

                  希望对你有帮助蒂埃里

                  这篇关于Angular 2 将组件动态附加到 DOM 或模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                    <tbody id='nT4H1'></tbody>

                        <bdo id='nT4H1'></bdo><ul id='nT4H1'></ul>
                        <tfoot id='nT4H1'></tfoot>

                        <i id='nT4H1'><tr id='nT4H1'><dt id='nT4H1'><q id='nT4H1'><span id='nT4H1'><b id='nT4H1'><form id='nT4H1'><ins id='nT4H1'></ins><ul id='nT4H1'></ul><sub id='nT4H1'></sub></form><legend id='nT4H1'></legend><bdo id='nT4H1'><pre id='nT4H1'><center id='nT4H1'></center></pre></bdo></b><th id='nT4H1'></th></span></q></dt></tr></i><div id='nT4H1'><tfoot id='nT4H1'></tfoot><dl id='nT4H1'><fieldset id='nT4H1'></fieldset></dl></div>

                        <small id='nT4H1'></small><noframes id='nT4H1'>

                          • <legend id='nT4H1'><style id='nT4H1'><dir id='nT4H1'><q id='nT4H1'></q></dir></style></legend>