1

I am new to angular2 and used a component which is from other library and I want to pass a data from angular2 . My problem is that , I can't set the variable if its not from angular2 to that third party component.

Can anyone enlighten me if its possible?

Ex.

//hmtl outside angular2
somepage.html
 var somevar; //variable on somepage.html

//component from angular2
export class SomeComponent implements OnInit{


constructor{
 //id like to assign a value to somevar from inside this component
 somevar = "blah blah";
}


}

Regards, Erwin

Erwin
  • 93
  • 1
  • 2
  • 9

2 Answers2

2

You can assign to window and then also access it from everywhere else this way:

constructor{
  //id like to assign a value to somevar from inside this component
  window.somevar = "blah blah";
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • having (index):42 Uncaught ReferenceError: somevar is not defined when i do that – Erwin Oct 18 '16 at 15:29
  • when i read it . I tried to alert it from somepage.html . Was expecting that variable will have a value. – Erwin Oct 18 '16 at 15:35
  • Then you obviously read it before the constructor that writes it is executed. – Günter Zöchbauer Oct 18 '16 at 15:36
  • Thanks for the reply. I agree it was loaded first before the constructor. – Erwin Oct 18 '16 at 17:37
  • You can dispatch a custom event to notify the external JS script that the value is available like shown in http://stackoverflow.com/questions/36342890/in-angular2-how-to-know-when-any-form-input-field-lost-focus/36348311#36348311 – Günter Zöchbauer Oct 18 '16 at 17:40
0

hmtl outside angular2

window['SomeVarUpdated'] = function (newValue) {
  // do something
};

component from angular2

export class SomeComponent {
  constructor() {
    window['SomeVarUpdated']("blah blah");
  }
}
yurzui
  • 205,937
  • 32
  • 433
  • 399