Search code examples
angularnativescriptnativescript-angularangular-moduleopenurl

How can I open a url for both Angular and Nativescript from one component codebase?


I am building a NativeScript-Angular app which displays a link than needs to open in a new browser on both the web and mobile.

For Angular, I can use window.open("http://example.com", "_blank"); - which does not work for NativeScript.

For NativeScript I can use utilityModule.openUrl(http://example.com); where var utilityModule = require("utils/utils"); - but this does not work with Angular (and it can't resolve and tns modules).

Is there any way I can use a single method in the my component to launch the default browser on a mobile and also open a new tab on a web browser?


Solution

  • Define a Service and have two implementations of it in your code you import "service"

    • service.ts: this will have the WEB version on how to open the link.
    • service.tns.ts: this will have the nativescript version on how to open the link.