2015-03-13 51 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

相關問題