2015-11-23 200 views
1

我正在使用基本的angular.js概念來顯示和隱藏div。由於某些原因,我無法使其工作。 上傳文件時,我根據registerStart值顯示div'myFormSpinner'。我正在創建一個手機差距應用程序。Angularjs範圍不起作用

有人可以幫我找到問題。除了顯示/隱藏div,一切正常。下面的代碼片段:

<div class="row" ng-controller="RegisterCtrl"> 
<div id="myFormSpinner" ng-show="registerStart"> 
    <img src="img/ajax-loader.gif"> 
</div> 
<div class="col-md-8"> 
    <form class="ng-pristine ng-invalid ng-invalid-required" style="margin-top:5%;"> 
      <div class="col-md-6"> 
        <input class="form-control " type="text" ng-model="registerData.Email" id="Email" name="Email" required placeholder="Email"> 
      </div> 
       <div class="col-md-offset-1 col-md-10 "> 
        <input type="submit" ng-click="register()" value="Register" class="btn btn-default btn-primary"> 
       </div> 
    </form> 
</div> 

event.controller('RegisterCtrl', ['$scope', '$state', '$q', '$http', 'eventService', 'authService', '$stateParams', '$rootScope', 'tokenService', 'imageService', 'spinnerService', 'appBackgroundService', 
function ($scope, $state, $q, $http, eventService, authService, $stateParams, $rootScope, tokenService, imageService, spinnerService, appBackgroundService) { 
var userNumber = 0; 
$scope.mediaUploadStart = false; 
$scope.eventId = $state.params.id; 

$scope.register = function() { 
    $scope.registerStart = true; 
    console.log('scope.Pic=' + $scope.pic); 
    if ($scope.registerData.Email) { 

     spinnerService.show('myFormSpinner'); 
     $scope.mediaUploadStart = true; 
     var paramOptions = { 
      eventId: $state.params.id, 
      email: $scope.registerData.Email, 
      fb: {}, 
      number: userNumber, 
      provider: "Form" 
     }; 
     uploadPicAndData(paramOptions).then(function (result) { 
      var data = JSON.parse(result); 
      $scope.registerStart = false; 
      appBackgroundService.disableBackgroundMode(); 
     }, 
      function (error) { 
       spinnerService.hide('myFormSpinner'); 
       $scope.registerStart = false; 
       appBackgroundService.disableBackgroundMode(); 
      }); 
     $scope.mediaUploadStart = false; 
     spinnerService.hide('myFormSpinner'); 
    } 
      }; 
}]) 
+0

我不是用這個語法,我建議這個https://github.com/johnpapa/angular-styleguide。嘗試將$ scope.registerStart放在註冊函數之外,然後在函數內切換它。 – brk

回答

1

您在異步模式下設置$範圍元素。 摘要循環無法確定在異步模式下對範圍元素進行了更改。

您應該使用$ apply來激活摘要循環。

$scope.$apply(function() { $scope.registerStart = false; }); 

瞭解更多關於$適用於此:http://jimhoskins.com/2012/12/17/angularjs-and-apply.html