Angular 5 : Dynamic Templates

标签 angular angular5

Angular 5 removed the runtime compiler ,我的理解是,因此现在无法动态编译模板。

所以我需要想出另一种方法来完成我的需要;希望有人能指出我正确的方向。

我有一个用 Angular 编写的视频平台。人们可以对视频发表评论,并可以在评论中的某处添加时间戳,例如:

Great video! Maybe a bit more colour at 00:32, make it look more like 01:20?

I'm matching each timestamp using Regex, and attempting to add a call to the component in the timestamp link:

const exp = /\b[0-9]*\:[0-9]*\b/;
comment.commentText = comment.commentText.replace(exp, (match) =>
{
     return '<a href="javascript:void(0);" (click)="jumpTo(\'' + match + '\')">' + match + '</a>';
});

由于现在缺少编译自定义模板的功能,我该如何让它工作?

提前致谢

最佳答案

可以创建一个 TaggedCommentComponent ,它接受原始评论文本作为输入,对其进行解析,并创建一个交错文本和时间标签链接的数组,以您的示例为基础,它将是:

[
  'Great video! Maybe a bit more colour at ',
  '00:32',
  ', make it look more like ',
  '01:20',
  '?'
]

然后在此组件的模板中使用如下内容:

<ng-container *ngFor="let t of texts; let even=even">
  <ng-container *ngIf="even">
    <a (click)="jumpTo(t)">{{t}}</a>
  </ng-container>
  <ng-container *ngIf="!even">{{t}}</ng-container>
</ng-container>

关于 Angular 5 : Dynamic Templates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47187963/

相关文章:

javascript - Angular 5 自定义错误处理程序 : Display Error-message

angular - 使垫子选项和垫子选择具有不同的用户界面

angular - 如何确保在 2 个或更多 Observable 返回数据之前不会执行函数?

Angular2 对 POST 请求的第一次响应未定义

Angular:具有使用 mat-select-filter 并向过滤器字段添加图标的搜索功能的多个 mat-select 下拉列表

Angular 5在两个组件之间共享函数和变量

angular - 如何在单击按钮或悬停时将 Angular Material (2.x +)对话框定位到屏幕的右侧

Angular 5 Http拦截器刷新JWT token

angular - 我可以在 Angular 2+ 中访问自定义 ControlValueAccessor 的 formControl 吗?

Angular 5 - HttpClient Post 不发布