How to create a link to external URL in Angular 2

后端 未结 1 1421
礼貌的吻别
礼貌的吻别 2021-02-19 09:39

I am new to Angular. I am starting with ver. 2.
I need to link to a file://... URL. I tried normal href:

Note: app is a m

相关标签:
1条回答
  • 2021-02-19 09:55

    I assume app is assigned async. You can work around this using the Elvis operator:

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

    to not break the binding when Angular tries to resolve it before app actually has a value.

    Original This worked for example:

    @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

    Actually, your first example works fine as well

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

    Plunker

    0 讨论(0)
提交回复
热议问题