2017-01-05 75 views
1

Stroy: 我正在做一個帶有appery.io的小型移動應用程序,它將掃描二維碼並根據值隱藏/顯示一個按鈕。帶回調更新Ionic UI元素

問題:(:隱藏名稱)布爾值:當我改變可變 按鈕將隱藏

$scope.QRscanner = function (_callback) { 
    cordova.plugins.barcodeScanner.scan(
      function (result) 
      { 
       if(result.cancelled!=1){ 

       $scope.hide = false; 
       $scope.scannedValue = result.text; 
       _callback(false); 
       } 
       else 
       { _callback(true); 
        alert("Operation cancelled"); 

       } 

      }, 
      function (error) { 
       $scope.hide = true; 
      }, 
      { 
       preferFrontCamera : true, // iOS and Android 
       showFlipCameraButton : true, // iOS and Android 
       showTorchButton : false, // iOS and Android 
       torchOn: false, // Android, launch with the torch switched on (if available) 
       prompt : "Place a barcode inside the scan area", // Android 
       resultDisplayDuration: 500, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500 
       formats : "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED 
       orientation : "landscape", // Android only (portrait|landscape), default unset so it rotates with the device 
       disableAnimations : true // iOS 
      } 
     ); 
} 

這是回調函數;

$scope.Callback = function (result) 
{ 
alert("result"+result); 
$scope.hide=result; 
} 

最後我會調用這個函數QRscanner在帶參數的回調函數名NG單擊

QRscanner(Callback); 

我很新的離子+ angular.kind幫助將不勝感激。

回答

1

假設你有以下兩個按鈕:

<button ng-click="QRscanner()"> Scan Code </button> 
<button ng-hide="hide"> Button to Hide </button> 

,如果你有兩個按鈕在同一個控制器是沒有必要送回調作爲參數,你只需要調用該函數:

$scope.hide = false; 

function Callback(result) { 
    alert("result" + result); 
    $scope.hide = result; 
} 

$scope.QRscanner = function() { 
    cordova.plugins.barcodeScanner.scan(
     function(result) { 
      if (result.cancelled != 1) { 

       $scope.hide = false; 
       $scope.scannedValue = result.text; 
       Callback(false); 
      } else { 
       Callback(true); 
       alert("Operation cancelled"); 

      } 

     }, 
     function(error) { 
      $scope.hide = true; 
     }, { 
      preferFrontCamera: true, // iOS and Android 
      showFlipCameraButton: true, // iOS and Android 
      showTorchButton: false, // iOS and Android 
      torchOn: false, // Android, launch with the torch switched on (if available) 
      prompt: "Place a barcode inside the scan area", // Android 
      resultDisplayDuration: 500, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500 
      formats: "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED 
      orientation: "landscape", // Android only (portrait|landscape), default unset so it rotates with the device 
      disableAnimations: true // iOS 
     } 
    ); 
} 
+0

感謝您的回覆;我已經修復這個,但有點不同的方式。我不得不打電話給$ scope。$ apply();在回調更新UI – mzonerz

+0

您可以添加ngCordova/Ionic Native到您的項目中,他們有AngularJS包裝,並更容易使用所有原生插件。使用ngCordova,您不需要手動執行apply(),因爲所有服務都會返回promise。你應該看看他們。 http://ngcordova.com/docs/ –