2015-03-13 50 views
2

我不知道为什么,但$范围不起作用的相机回调。 (的onSuccess功能)AngularJS + PhoneGap相机 - 我怎样才能获得成功的范围

HTML

<button ng-click="capturePhoto();">Capture</button> 
<span>{{ test }}</span> 

JAVASCRIPT

app.controller('myController', function($scope, $http) { 

    $scope.capturePhoto = function(){ 

     $scope.test = "test 1"; 

     navigator.camera.getPicture(onSuccess, onFail, { quality: 50, 
     destinationType: Camera.DestinationType.DATA_URL }); 

    } 

    function onSuccess(imageData) { 

     var image = imageData; 

     alert($scope); // [object Object] 
     alert($scope.test); // test1 
     $scope.test = "test 2"; // Problem: do not show on screen 
     alert($scope.test); // test2 

    } 

}); 

的页面仍呈现TEST1。难道我做错了什么?有没有最好的方法来做到这一点?

感谢

回答

5

,因为你出的角度与插件回调消化周期实在不行,角度只是不知道,有则改之,而不能更新。

最简单的方法是使用$适用于:

function onSuccess(imageData) { 

    $scope.$apply(function(){ 
     var image = imageData; 

     alert($scope); // [object Object] 
     alert($scope.test); // test1 
     $scope.test = "test 2"; // Problem: do not show on screen 
     alert($scope.test); // test2 
    }); 

} 

在我看来,最好的办法是用承诺:

app.controller('myController', function($scope, $http, $q) { 

$scope.capturePhoto = function(){ 

    $scope.test = "test 1"; 
    var defer = $q.defer(); 
    defer.promise.then(function (imageData){ 
     var image = imageData; 

     alert($scope); // [object Object] 
     alert($scope.test); // test1 
     $scope.test = "test 2"; // Problem: do not show on screen 
     alert($scope.test); // test2 
    }, function (error){}); 

    navigator.camera.getPicture(defer.resolve, defer.reject, { quality: 50, 
    destinationType: Camera.DestinationType.DATA_URL }); 

} 
+0

我只是尝试的第一个选项,并担任一个魅力。我会在稍后尝试第二个选项。 – guinatal 2015-03-13 16:32:29

相关问题