Angular 6,带有href的标签使用#link链接到基本页面而不是当前页面

时间:2018-10-03 07:14:46

标签: html angular innerhtml

我当前面临的问题是a标记生成的链接链接到基础页面。如您在图片中所见,它链接到

  

localhost:3000#hello

我的目标是使其链接到

  

localhost:3000 / bodyText#hello

标签将来自外部来源,因此我的测试示例对此进行了模仿。到目前为止,我一直在使用innerHTML指令将外部html放在html模板中。

这是我正在使用的组件

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-test',
template: '<div [innerHTML]=html></div>',
 styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {

constructor() { 
}

html = "<a href=\"#hello\" title=\"hello\">A tag </a> <a name=\"hello\" id= \"hello\"/> "

ngOnInit() {
}

}

This is what my page currently looks like

1 个答案:

答案 0 :(得分:0)

我通过在html标记中添加点击处理程序,然后使用scrollIntoViewgetElementById而不是使用标记来解决此问题。