lipidfish
lipidfish

Reputation: 406

Angular 2 referencing external DOM elements

I would like to use Angular 2 for augmenting an existing aspx page. The page has a couple of fields that contain json data, and I would like to be able to use that json inside my Angular 2 components.

For example, I have an index.html that looks like this:

<my-ng-app></my-ng-app>
<input type="hidden" id="student" value="{name: 'bill'}" />

Is there any way I can get the value from that hidden field into my ng2 components?

Upvotes: 0

Views: 339

Answers (1)

silentsod
silentsod

Reputation: 8335

You can use direct JavaScript to do it:

let student = document.getElementById("student").value;

If you need to parse the value into an object, JSON.parse(student) will do it for you

Or you can use Document from @angular/platform-browser like so:

import { DOCUMENT } from "@angular/platform-browser";

constructor(@Inject(DOCUMENT) private document: any) {
}

ngOnInit() {
    this.document.querySelector("#student").value;
}

Upvotes: 2

Related Questions