【问题标题】:What is the alternative for "ng-include" in angular2?angular2中“ng-include”的替代方法是什么?
【发布时间】:2018-09-02 13:58:36
【问题描述】:

在 angular 中是否有任何替代方法来实现 ng-include 在 angularjs 中的作用?

【问题讨论】:

标签: angular


【解决方案1】:

最接近 ng-include 的是 ngTemplateOutlet 指令。您需要将 TemplateRef 和可选上下文传递给它。类似的东西:

@Component({
  selector: 'child',
  template: `
    <div>
      here is child template that includes myTemplate
      <ng-container [ngTemplateOutlet]="myTemplate"></ng-container>
    </div>`
})
export class ChildComponent {
  @Input() myTemplate: TemplateRef<any>;
}


@Component({
  selector: 'app-root',
  template: `
    <p>Parent</p>
    <child [myTemplate]="myTemplate"></child>
    <ng-template #myTemplate>hi julia template!</ng-template>
  `
})
export class AppComponent {
  @ViewChild('myTemplate', {read: TemplateRef}) myTemplate: TemplateRef<any>;
}
  1. 父组件查询模板并将其传递给子组件
  2. 子组件使用 ngTemplateOutlet 指令创建视图并渲染它。

【讨论】:

    【解决方案2】:
    //ng-include equivalent in Angular2/4
    // How to create directive for ng-clude in Angular2/4
    import {
        Directive,
        ElementRef,
        Input,
        OnInit
    } from '@angular/core';
    import {
        Headers,
        Http,
        Response
    } from '@angular/http';
    
    @Directive({
        selector: 'ngInclude'
    })
    export class NgIncludeDirective implements OnInit {
    
        @Input('src')
        private templateUrl: string;
        @Input('type')
        private type: string;
    
        constructor(private element: ElementRef, private http: Http) {
    
        }
        parseTemplate(res: Response) {
            if (this.type == 'template') {
                this.element.nativeElement.innerHTML = res.text();
            } else if (this.type == 'style') {
                var head = document.head || document.getElementsByTagName('head')[0];
                var style = document.createElement('style');
                style.type = 'text/css';
                style.appendChild(document.createTextNode(res.text()));
                head.appendChild(style);
            }
        }
        handleTempalteError(err) {
    
        }
        ngOnInit() {
            this.
            http.
            get(this.templateUrl).
            map(res => this.parseTemplate(res)).
            catch(this.handleTempalteError.bind(this)).subscribe(res => {
                console.log(res);
            });
        }
    
    }
    
    enter code here
    
        // html code
    
        <
        ngInclude src = "{{src}}"
    type = "template" > < /ngInclude>
    

    【讨论】:

      【解决方案3】:

      从 angular2+ 的角度思考,最好将子模板声明为组件:

      @Component({
        selector: 'app-child', 
        template: `
          <ng-container>
            here is child template that includes myTemplate
          </ng-container>`
      })
      export class ChildComponent {
      }
      
      
      @Component({
        selector: 'app-root',
        template: `
          <p>Parent</p>
          <app-child ></app-child>
        `
      })
      export class AppComponent {
      }
      

      【讨论】:

        猜你喜欢
        • 2016-09-06
        • 2016-10-08
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        • 2022-11-13
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多