栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Angular 2中的动态模板URL

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Angular 2中的动态模板URL

尽管可能不是最优雅的解决方案,但我使用DynamicComponentLoader和ElementRef将模板值动态分配给组件。实际上,我在寻找一种可以在占位符中添加多个自定义组件的解决方案。

我尝试了shmck概述的函数中的服务注入,由于调用模板函数时服务尚不可用,因此无法使用。确实,

this
是指Window对象。

简而言之:

一个简单的组件作为要使用的模板

@Component({  selector: 'dt2-simple-block',  properties: ["idx"],  template: `<h1>Simple block for  {{ idx }} </h1>`,  directives: []})class dt2SimpleBlock {  constructor() {  }}

包含所有要添加的组件的Component的构造方法(我的应用程序要求包含多个子元素:

 constructor(loader: DynamicComponentLoader, elementRef: ElementRef) {  //iterate  for (var i = 0; i < toSomething; i++) {      // build the template      var blockdirective = 'dt2-simple-block'      var template = '<' + blockdirective +' idx="' + this.userBlocks.userHomePanelBlocks[i] +          '"></' + blockdirective + '>';      console.log(template);   // debugging purpose      var directives = [dt2SimpleBlock];        loader.loadNextToLocation(toComponent(template, directives), elementRef);    }

并将辅助功能作为util放置在某处

function toComponent(template, directives = []) {  @Component({ selector: 'fake-component' })  @View({ template, directives })  class FakeComponent { }  return FakeComponent;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号