How to use nativescript's WebView in angular2?

若如初见. 提交于 2019-12-01 09:40:44
Dlucidone

Your .ts file containing class implementation

import { Component } from "@angular/core";

@Component({
selector: "Date",
templateUrl: "myPage.html", //here is your reference to html page
})

export class Angular2WebView {
//any class function
}

Your html file myPage.html

<GridLayout>
    <WebView id="webView" url="http://www.google.com" >
    </WebView>
</GridLayout>

for more info. refer here Nativescript bare webview doesnt seem to work

Good link for documentation for using WebView Nativescript/Android

html:

 <WebView #webview [src]="url"></WebView>

backend code:

import {Component, OnInit} from "@angular/core";
import {WebView, LoadEventData} from "ui/web-view";

@Component({
    selector: 'basic-web-view-component',
    templateUrl: //see html above
})

export class BasicWebViewComponent implements OnInit {

    public url="https://www.google.com";
    @ViewChild("webview") webview: WebView;

    ngOnInit() {

        this.webview.on(WebView.loadFinishedEvent, function (args: LoadEventData) {
             console.log("loadFinishedEvent called");
        });
    }
}

Note: that it seems that WebView needs to sized with width/height attributes are in a GridView, see the docs reference above. It won't dynamically grow based on content.

Richard Lee

I solved it with the following code:

<WebView [src]="url" (loadFinished)="loadFinished($event)"></WebView>

Important: remove the <Page> tag (should not be used within Nativescript/Angular 2).

More info: github issue

Have you tried to use the eventargs on load with the method getViewById to find a control. You can find more information here - https://docs.nativescript.org/ui/ui-with-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!