Add element with RouterLink dynamically

后端 未结 3 2059
悲&欢浪女
悲&欢浪女 2020-12-10 15:20

When i put an anchor element in someweher in a angular 2 Component like this,

Static Link
         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 15:42

    routerLink is a directive. Directives and Components are not created for HTML that is added using [innerHTML]. This HTML is not process by Angular in any way.

    The recommended way is to not use [innerHTML] but DynamicComponentLoaderViewContainerRef.createComponent where you wrap the HTML in a component and add it dynamically.

    For an example see Angular 2 dynamic tabs with user-click chosen components

提交回复
热议问题