gpt4 book ai didi

hyperlink - 如何在 Angular 2 中创建指向外部 URL 的链接

转载 作者:太空狗 更新时间:2023-10-29 17:04:55 32 4
gpt4 key购买 nike

我是 Angular 的新手。我从版本开始。 2.
我需要链接到 file://... URL。我尝试了正常的 href:

注意:app 是处理应用程序的网络模型对象。

<a target="_blank" href="file://{{app.outputPath}}/index.html">no link here</a>.

那是行不通的——链接在那里,URL 正确,但 Angular 似乎以某种方式阻止了事件。为什么?

所以我看过 ng-href 但那是针对 Angular 1.x 的。和 there's no *ngHref据我所知。所以这只是一个天真的尝试:

<a target="_blank" *ngHref="file://{{app.outputPath}}/index.html">over a directive</a>.

我还看到了一些带有路由的东西,但它似乎只用于应用程序中的内部链接:

<a [router-link]="['/staticReport', {path: app.outputPath}]">see the report</a>.

app.component.ts:

@RouteConfig([
...
{path:"/staticReport/:path", redirectTo: 'file:// ???? ' }
])

创建外部链接的方法是什么?

最佳答案

我假设 app 被分配了异步。您可以使用 Elvis operator 解决此问题:

<a target="_blank" href="file://{{app?.outputPath}}/index.html">no link here</a>.

当 Angular 尝试在 app 实际具有值之前解析它时,不要破坏绑定(bind)。

原创这适用于例如:

@Component({
selector: 'my-app',
template: `
<h2>Hello {{name}}</h2>
<a target="_blank" [href]="'file://' + outputPath + '/index.html'">no link here</a>
`
})
export class App {
outputPath:string = 'www.google.com';

constructor() {
this.name = 'Angular2';
}
}

Plunker

实际上,您的第一个示例也可以正常工作

<a target="_blank" href="file://{{outputPath}}/index.html">no link here</a>

Plunker

关于hyperlink - 如何在 Angular 2 中创建指向外部 URL 的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36082785/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com