简体   繁体   English

Angular2根据服务响应动态选择模板

[英]Angular2 choose template dynamically according to service response

I have a component called "Node" which should display a single post, but I have more than a post type and each type has its own view (layout). 我有一个名为“Node”的组件,它应该显示一个帖子,但我有一个以上的帖子类型,每个类型都有自己的视图(布局)。

So this component has an ID parameter which will be used to get a post response from a service then according to post type it should display the correct layout. 所以这个组件有一个ID参数,用于从服务获得post响应,然后根据post类型显示正确的布局。

ex: localhost/test_app/node/123 where 123 is the id parameter. 例如: localhost/test_app/node/123其中123是id参数。

Approach 1: using ngIf in the tamplate 方法1:在模板中使用ngIf

@Component({
  selector: 'node',
  directives: [Post, Project],
  template: `
    <post *ngIf="response.post.post_type == 'post'" content="response.post"></post>
    <project *ngIf="response.post.post_type == 'project'" content="response.post"></project>
  `,
})

export class Node{

  id: number;
  response: any;
  constructor(private _param: RouteParams,
              public service: Service
  ){
    this.id = +_param.get('id');
  }

  ngOnInit(){
    this.service.get(this.id).subscribe(
      res=> this.response = res.json()
    );
  }

}

Approach 2: using dynamic component loader. 方法2:使用动态组件加载器。

@Component({
  selector: 'node',
  directives: [Post, Project],
  template: '<div #container></div>'
})

export class Node{

  id: number;
  response: any;

  constructor(private _param: RouteParams,
              public service: Service,
              private loader:DynamicComponentLoader,
              private elementRef:ElementRef
  ){
    this.id = +_param.get('id');
  }

  ngOnInit(){
    this.service.get(this.id).subscribe(
      res=> {
          this.response = res.json();
          this.renderTemplate();
      }
    );
  }

  renderTemplate(template) {
    this.loader.loadIntoLocation(
      this.getCorrectTemplate(),
      this.elementRef,
      'container'
    )
  }

  getCorrectTemplate(){
    if(this.response.post.post_type == "post"){
        return '<post content="response.post"></post>';
    }
    else{
        return '<project content="response.post"></project>';
    }
  }

}

I'm wondering which approach is more efficient or if there is a better approach, please share it. 我想知道哪种方法更有效,或者如果有更好的方法,请分享。

If you have a limited set of possible templates then *ngIf or *ngSwitch is a good approach. 如果你有一组有限的模板,那么*ngIf*ngSwitch是一个很好的方法。 It allows you to use [] and () bindings between parent and child. 它允许您在父和子之间使用[]()绑定。

If you have a set of templates that are statically unknown and for example provided by a parameter then *ngIf doesn't work. 如果您有一组静态未知的模板,例如由参数提供,则*ngIf不起作用。 Then ViewContainerRef.createComponent() (replaces DynamicComponentLoader ) is the right approach. 然后ViewContainerRef.createComponent() (替换DynamicComponentLoader )是正确的方法。 For an example see Angular 2 dynamic tabs with user-click chosen components 有关示例,请参阅Angular 2动态选项卡,其中包含用户单击所选组件

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

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