我用离子框架做了一个应用程序。我想使用从本机java调用的javascript函数更新我的html进度条,而我的java在asynctask上运行。
首先,这是我的角度控制器:
.controller('DMCtrl', function($scope) {
$scope.updateProgress = function(name,progress){
loader.setProgress(progress); //loader is my progress bar in view
};
});
这是我的java调用updateProgress:
private void updateProgress(final String name, final int progress, final int max){
mGap.runOnUiThread(new Runnable(){
@Override
public void run() {
double data = (double)progress/(double)max;
mGap.loadUrl("javascript: updateProgress('"+name+"',"+"'"+data+"');"); //mGap is my cordova activity
}
});
}
我在asynctask中调用该方法并成功运行。但是logcat显示错误,我的javascript函数updateProgress未定义,我的应用程序变为黑屏。看起来它调用空白页而不是调用我的javascript函数。这里是日志:
04-01 14:43:20.197: I/DataAdapter(12328): Start getPXPreservedList(52025900) >> Wed Apr 01 14:43:20 GMT+07:00 2015
04-01 14:43:20.198: E/dalvikvm(12328): [DVM] mmap return base = 48159000
04-01 14:43:20.211: D/dalvikvm(12328): GC_FOR_MALLOC freed 398K, 60% free 3101K/7687K, external 0K/2560K, paused 14ms
04-01 14:43:20.215: I/System.out(12328): [socket][2] connection 192.168.1.57/192.168.1.57:8080;LocalPort=47448(20000)
04-01 14:43:20.215: I/System.out(12328): 192.168.1.57/192.168.1.57:8080(20000)
04-01 14:43:20.215: I/OSNetworkSystem(12328): OSNetworkSystem_connect fd=-1; timeout = 20000
04-01 14:43:20.217: I/System.out(12328): Socket[addr=/0.0.0.0,port=0,localport=47448]
04-01 14:43:20.217: I/System.out(12328): [socket][/192.168.1.50:47448]
04-01 14:43:20.283: I/System.out(12328): close [socket][/0.0.0.0:47448]
04-01 14:43:20.338: D/CordovaWebView(12328): >>> loadUrl(javascript: updateProgress('pxconfig','0.13636363636363635');)
04-01 14:43:20.338: D/PluginManager(12328): init()
04-01 14:43:20.344: I/webkit/webview(12328): Webview.loadUrl() this=org.apache.cordova.CordovaWebView@40534fc0url: javascript: updateProgress('pxconfig','0.13636363636363635');
04-01 14:43:20.347: D/CordovaLog(12328): : Line 1 : Uncaught ReferenceError: updateProgress is not defined
04-01 14:43:20.347: E/Web Console(12328): Uncaught ReferenceError: updateProgress is not defined at :1
答案 0 :(得分:0)
我的建议是在Ionic发出一个事件并对该事件实施动作。应该有用jsfiddle.net/hxtpoe/uvs103gy
angular.element(document).ready(function () {
angular.bootstrap(document, ['App']);
});
angular.module('App', [])
.controller('Controller', ['$document', '$scope', function($document, $scope) {
$scope.user = {'name' : "Hello"};
$document.on('myevent', function() {
$scope.$apply(
function() {
$scope.user.name = "hello my event!";
});
})
}]);
// end of angular/ionic app
var button = document.getElementById("emit");
button.onclick = function() {
var event = new CustomEvent("myevent", {});
document.dispatchEvent(event);
};