Search code examples
angularhref

Angular 2 variable in template is undefined


I am declaring a variable in my component.

E.g.

let link = 'https://google.com'

But if I use it in my template, it is not there

E.g

{{ link }} -> has no output
href="#{{link}}" -> resolved into href="#"
[attr.href]="link" -> resolves into nothing
[attr.href]="link+'#'" -> resolves into href="undefined/#"

What am I doing wrong?

Thanks for your help


Solution

  • Change

    let link = 'https://google.com'
    

    to

    link:string = 'https://google.com';
    

    This code

    let link = 'https://google.com'
    

    is a statement and can only be used within the constructor or a method or function, but not at a class' top-level where only declarations are allowed.