When developing a web application with jQuery or plain JavaScript, it is usually necessary to check for features. For example, if I want to use the document.oncopy event, I must first have something like this to ensure that my code does not interrupt for smaller browsers:
if ("oncopy" in document) { // Feature is available }
I am a little puzzled by how this works in Angular2. I could still use the same if I expected you to run only in the browser, but I was just told to leave only the DOM if I want to use Angular Universal and instead depend on templates or DomRenderer. This allows you to pre-display the page on the server and provides a truly impressive increase in performance.
But suppose I want a particular div to be invisible if document.oncopy not available. I understand that this is not recommended:
<div *ngIf="hasFeature()">...</div>
and
hasFeature() { return 'oncopy' in document; }
because then I'm still manipulating the DOM. Please note that my example refers to document.oncopy , but I could choose any function that does not have universal support.
I tested this using Chris Nwamba 's Scotch tutorial and added the following to the end of my home template:
<div *ngIf="hasFeature()">Feature is supported</div> <div *ngIf="!hasFeature()">Feature is NOT supported</div>
Update: Interestingly, it gave different results in different browsers. On Chrome 55, it ran as usual and showed the message โFeature Supportedโ. In IE11, I received a "not supported" message. In both cases, the server log shows the message EXCEPTION: document is not defined , but the page still looks fine.
So, what is the correct way to test browser functions if I want to use Angular Universal?
Update: I also played using the field in the template and assigned this field from one of