Angular 2将服务注入动态组件

时间:2016-08-26 19:32:53

标签: angular

我正在创建一个多步骤用户注册,在每个步骤之后,服务器返回下一个表单的HTML字符串以完成。在每一步,我将HTML设置为我使用CompileService动态创建的表单组件的模板。这工作正常,表单看起来不错,但我无法向动态创建的表单添加任何服务,我不断遇到类似

的问题

"Cannot resolve all parameters"

这是我的CompileService

import { Component, ComponentMetadata, ComponentResolver, Injectable, ReflectiveInjector, ViewContainerRef } from '@angular/core';

@Injectable()
export class CompileService {
constructor(private resolver: ComponentResolver) {}

createComponent(metadata: ComponentMetadata, vcRef: ViewContainerRef) {
        let cmpClass = class DynamicComponent {};

        let decoratedCmp = Component(metadata)(cmpClass);
        this.resolver.resolveComponent(decoratedCmp).then(factory => {
        let injector = ReflectiveInjector.fromResolvedProviders(providers, vcRef.parentInjector);
            vcRef.createComponent(factory, 0, injector, []);
        });
    }
}

我希望编辑此服务,以便它不仅可以即时创建组件,还可以将服务注入到该组件中。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

查看这行代码,请解释提供商参数是什么......

       <!-- Navigation -->
        <!-- Fixed navbar -->
        <nav id="header" class="navbar navbar-fixed-top">
        <div id="header-container" class="container navbar-container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a id="brand" class="navbar-brand" href="/"><img src="https://www.inciner8.com/images/full-logo-white.png" width="160" height="25"></a>
        </div>
        <div id="navbar" class="collapse navbar-collapse pull-right">
          <ul class="nav navbar-nav">
            <li class="dropdown mega-dropdown">
            <a href="#incinerators" class="dropdown-toggle" data-toggle="dropdown">INCINERATORS <b class="caret"></b></a>

            <ul class="dropdown-menu mega-dropdown-menu row">
                <li class="col-sm-3">
                  <ul>
                    <li class="dropdown-header">Most Popular</li>
                    <div id="myCarousel" class="carousel slide" data-ride="carousel">
                      <div class="carousel-inner">
                        <div class="item active">
                          <a href="#"><img src="http://placehold.it/254x150/3498db/f5f5f5/&text=i8M-60+Medical" class="img-responsive" alt="product 1"></a>
                          <h4><small>Self-contained Medical Incinerator</small></h4>
                          <button class="btn btn-primary" type="button">GET a Quote</button>
                          <button href="#" class="btn btn-default" type="button"><span class="glyphicon glyphicon-heart"></span> more info...</button>
                        </div>
                        <!-- End Item -->
                        <div class="item">
                          <a href="#"><img src="http://placehold.it/254x150/ef5e55/f5f5f5/&text=Large+Capacity" class="img-responsive" alt="product 2"></a>
                          <h4><small>Our Flagship 1 Tonne Incinerator</small></h4>
                          <button class="btn btn-primary" type="button">GET a Quote</button>
                          <button href="#" class="btn btn-default" type="button"><span class="glyphicon glyphicon-heart"></span> more info...</button>
                        </div>
                        <!-- End Item -->
                        <div class="item">
                          <a href="#"><img src="http://placehold.it/254x150/2ecc71/f5f5f5/&text=Dual+Chamber" class="img-responsive" alt="product 3"></a>
                          <h4><small>Hot Hearth Designed Dual Chamber Pet Cremator</small></h4>
                          <button class="btn btn-primary" type="button">GET a Quote</button>
                          <button href="#" class="btn btn-default" type="button"><span class="glyphicon glyphicon-heart"></span> more info...</button>
                        </div>
                        <!-- End Item -->
                      </div>
                      <!-- End Carousel Inner -->
                    </div>
                    <!-- /.carousel -->
                  </ul>
                </li>

              </ul>

            </li>



            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">PET CREMATION <b class="caret"></b></a>
                      <ul class="dropdown-menu">
                        <li><a href="#">Dual Chamber Pet Cremator <span class="badge badge-important pull-right">New!</span></a></li>
                        <li><a href="#">Horse Cremator</a></li>
                        <li><a href="#">Heat Exchangers</a></li>
                        <li><a href="#">Pet Cremation Accessories</a></li>
                        <li class="divider"></li>
                        <li class="dropdown-header1">Want to learn more?</li>
                        <li><a href="#">Starting a Pet Crematorium</a></li>
                        <li><a href="#">Pet Cremation Business Plan</a></li>
                        <li><a href="#">Pet Cremation Business Franchise</a></li>
                      </ul>
            </li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">WASTE TO ENERGY <b class="caret"></b></a>
                      <ul class="dropdown-menu">
                        <li><a href="#">EFW-40</a></a></li>
                        <li><a href="#">EFW-20</a></li>
                        <li><a href="#">Heat Exchangers</a></li>
                        <li><a href="#">Organic Ranking Cycle Engine</a></li>
                        <li class="divider"></li>
                        <li class="dropdown-header1">Want to learn more?</li>
                        <li><a href="#">What is W2E?</a></li>
                        <li><a href="#">Use Incinerators to Create Electricity</a></li>
                      </ul>
            </li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">POLLUTION CONTROL <b class="caret"></b></a>
                      <ul class="dropdown-menu">
                        <li><a href="#">Small PCS</a></li>
                        <li><a href="#">Large PCS</a></li>
                        <li><a href="#">Venturi Scrubber</a></li>
                        <li class="divider"></li>
                        <li class="dropdown-header1">Want to learn more?</li>
                        <li><a href="#">Ceramic Filters</a></a></li>
                        <li><a href="#">Activated Carbon Filters</a></li>
                        <li><a href="#">Lime Feed</a></li>
                        <li><a href="#">What are Acceptable Emission Levels?</a></li>
                      </ul>
            </li>
             <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">INCINERATION <b class="caret"></b></a>
                      <ul class="dropdown-menu">
                        <li><a href="#">How it Works</a></a></li>
                        <li><a href="#">Types of Incinerator</a></li>
                        <li><a href="#">What you Can/Cannot Burn</a></li>
                        <li><a href="#">Legislation</a></li>
                        <li class="divider"></li>
                        <li class="dropdown-header1">Want to Learn More?</li>
                        <li><a href="#">Read our Blog...</a></li>
                      </ul>
            </li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">SUPPORT <b class="caret"></b></a>
                      <ul class="dropdown-menu">
                        <li><a href="#">Download Brochures & Datasheets <span class="badge badge-important pull-right">New!</span></a></li>
                        <li><a href="#">Emissions Quick Reference</a></li>
                        <li><a href="#">Fuel Consumption Quick Reference</a></li>
                        <li><a href="#">Dealer Resources</a></li>
                        <li class="divider"></li>
                        <li class="dropdown-header1">Useful Resources</li>
                        <li><a href="#">Visit our <strong>spare parts website</strong></a></li>
                        <li><a href="#">Activate your Warranty</a></li>
                        <li><a href="#">Become a Dealer</a></li> 
                        <li><a href="#">Become a Dealer</a></li>     
                        <li class="divider"></li>    
                        <li class="dropdown-header1">FREE Waste Audit Templates <span class="badge badge-important pull-right">New!</span></li>    
                        <li><a href="#">Waste Audit</a></li>
                        <li><a href="#">Waste Logbook</a></li>   
                        <li><a href="#">Medical Waste Logbook</a></li>  
                        <li><a href="#">Animal Waste Logbook</a></li>    
                      </ul>
            </li>

            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">ABOUT <b class="caret"></b></a>
                      <ul class="dropdown-menu">
                        <li><a href="#">Company History</a> <span class="badge badge-important pull-right">New!</span></li>
                        <li><a href="#">Awards & Accreditations</a></li>
                        <li><a href="#">The Team</a></li>
                        <li><a href="#">News</a></li>
                        <li><a href="#">Our Values</a></li>
                      </ul>
            </li>



            <li><a href="#contact" class="btn-primary" type="button">CONTACT</a></li>
          </ul>
        </div><!-- /.nav-collapse -->
      </div><!-- /.container -->
    </nav>
    <!-- /.navbar -->

我无法分辨它是什么或来自哪里。在这种情况下,我会告诉你......

  

“无法解析所有参数”