1
我有一个问题,我需要一些帮助。我试图通过在我的前端使用角度控制器来调用一个android函数,但是我无法使它工作。从AngularJS控制器调用Android功能
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView)findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new AngularJSInterface(this), "Android");
mWebView.loadUrl("http://192.168.70.101:3000/");
}
AngularJSInterface:
public class AngularJSInterface {
Context mContext;
AngularJSInterface(Context c) {
mContext = c;
}
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
角控制器:
angular.module('app').controller('ComplaintCtrl', function ($scope, $http, complaintService) {
$scope.showToast = function(toast) {
Android.showToast(toast);
console.log("toast");
}
});
的HTML:
<button label="toast" ng-click="showToast('Visar toast från angularJS')">toast</button>
从控制台错误:
ReferenceError: Android is not defined
at Scope.$scope.showToast (http://localhost:3000/scripts/controllers/addcomplaint.js:34:3)
at http://localhost:3000/bower_components/angular/angular.js:10567:21
at http://localhost:3000/bower_components/angular-touch/angular-touch.js:438:9
at Scope.$eval (http://localhost:3000/bower_components/angular/angular.js:12412:28)
at Scope.$apply (http://localhost:3000/bower_components/angular/angular.js:12510:23)
at HTMLButtonElement.<anonymous> (http://localhost:3000/bower_components/angular-touch/angular-touch.js:437:13)
at HTMLButtonElement.jQuery.event.dispatch (http://localhost:3000/bower_components/jquery/dist/jquery.js:4409:9)
at HTMLButtonElement.elemData.handle (http://localhost:3000/bower_components/jquery/dist/jquery.js:4095:28)
这是我第一次使用这样的事情,我不太清楚如何真正做到这一点。我尝试通过Google搜索寻找答案,但没有运气。我究竟做错了什么?
我已将showToast方法更改为static,并用AngularJSInterface替换了Angular.showToast()。 showToast(),尽管我仍然得到相同的错误(AngularJSInterface未定义)。我该如何实例化它? – CobraAn 2014-11-04 11:35:57
AngularJSInterface myInterface = new AngularJSInterface(); myInterface.showToast(); – 2014-11-04 15:29:49