2

My environment is webview, and I want to call Angular's function from Java, so I have to find out the function. How can I do ?

molysama
  • 107
  • 1
  • 9
  • Possible duplicate of [Call a service function from browser console in angular 6](https://stackoverflow.com/questions/54212288/call-a-service-function-from-browser-console-in-angular-6) – ReturnTable Jul 11 '19 at 02:08

3 Answers3

3

In order to get the JavaScript function’s return value, you need to use the evaluateJavascript method:

webView.evaluateJavascript("javascript:myTestFunction();", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
            // Do what you want with the return value
        }
    });

1) Calling Method of Java application from Angular2+ application:-

Suppose, JavaFunction.SomeJavaMethod() is defined in Java app then,

.ts

 let data = {
    'key' : 2
    }
    JavaFunction.SomeJavaMethod(data);// call from angular

2) Calling Angular method from webview/JavaApplication:-

index.html

<body>
    <app-root></app-root>
    <script type="text/javascript">
         // data send from webview java app...call from java app
        function updateFromJAVAapp(data) {
            window['componentRef'].zone.run(() => {
                window['componentRef'].component.updateFromMyJavaApp(data);
            });
        }

    </script>
</body>

.ts

 constructor(
        private zone: NgZone
    ) {
        window['componentRef'] = {
            zone: this.zone,
            componentFn: (value) => this.updateFromMyJavaApp(value),
            component: this
        };
    }

    updateFromMyJavaApp(data) {
        console.log( JSON.stringify(data)); <=== data passed from java app webview
    }

Refer Link:-

https://www.tanelikorri.com/tutorial/android/communication-between-application-and-webview/

http://shripalsoni.com/blog/nativescript-webview-native-bi-directional-communication/

Mahi
  • 3,748
  • 4
  • 35
  • 70
1

On browser, click on the html tag represents Component you want to find.

enter image description here

Then ng.probe($0).componentInstance

enter image description here

Lam Nguyen
  • 186
  • 1
  • 8
1
ngOnInit{
 (window as any).name_visible_from_console = this.class_function.bind(this);
}

i think it's good to clear it in ngOnDestory

you also need to use ApplicationRef#tick in called method, cause angular won't update view

Damian Pioś
  • 473
  • 2
  • 5