-1

How it's possible to component (ts file) get access to a variable inside index.html? exemple:

inside: index.html <javascript> var myVar = "test"; </javascript>

how to get this variable "myVar" to be used inside a component page

to be used for example on the component html like {{myVar}}

  • It is not possible. that piece of JS might be in a totally different bundle than the script-code in your components – Phil May 10 '18 at 17:43

1 Answers1

0

Inside the ts file, you can declare it like this :

//Other imports
declare var myVar: any;

@Component({
selector: 'my-component',
templateUrl: './my-component.component.html',
 styleUrls: ['./my-component.component.scss']
})

export class MyComponent {
  my_javascript_variable = myVar;
}
Prachi
  • 3,478
  • 17
  • 34
  • Perfect. works fine. Are there any listerner to await changes for this variable ? – Uraquitan Matova Ura Kombat May 10 '18 at 18:00
  • Please refer to this https://stackoverflow.com/questions/36828856/angular2-watch-an-external-variable-outside-of-angular. They have used it like window.myVar, instead of declaring it into the component. – Prachi May 10 '18 at 18:04