【发布时间】:2019-10-24 13:30:39
【问题描述】:
我在Android中有一个JS桥,
public class WebInterface {
Context mContext;
public WebInterface(Context c) { this.mContext = c;}
@JavascriptInterface
public void showAlert() {
Toast.makeText(mContext, "This is being called from the interface", Toast.LENGTH_SHORT).show();
}
}
我可以在我的webView中设置界面,
mWebView.addJavascriptInterface(WebInterface(this), "android");
这适用于像showAlert() 这样的简单方法,其中当参数或参数是简单字符串时没有参数,但是当我需要在从 Web 应用程序调用本机函数时将数据模型作为参数传递时,我该怎么做绑定数据模型?我需要调用实现具有自定义数据模型类型参数的函数。
public class WebInterface {
Context mContext;
public WebInterface(Context c) { this.mContext = c;}
@JavascriptInterface
public void showAlert() {
Toast.makeText(mContext, "This is being called from the interface", Toast.LENGTH_SHORT).show();
public void saveData(data: DataModel) { // DataModel is custom model
Toast.makeText(mContext, "Saving data model", Toast.LENGTH_SHORT).show();
}
}
如何跨原生应用和网络应用绑定数据模型。可以使用 TypeScript 吗?如果可以,如何配置?是否只能使用纯 json 字符串作为参数?没有别的办法吗?
【问题讨论】:
标签: javascript android angularjs typescript web