【问题标题】:How to create a link to external URL in Angular 2如何在 Angular 2 中创建指向外部 URL 的链接
【发布时间】:2016-07-05 02:43:31
【问题描述】:

我是 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://  ???? ' }
])

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

【问题讨论】:

  • 您是否检查过在 DOM 中生成的 URL 是什么样子的? (“检查元素”链接上的上下文菜单)。实际上,“app 是处理应用程序的网络模型对象”并没有提供太多信息。

标签: hyperlink angular external angular2-routing angular2-directives


【解决方案1】:

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

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

当 Angular 尝试在 app 实际具有值之前解决绑定时,不要破坏绑定。

原创 例如:

@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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多