簡體   English   中英

如何在查看angularjs之前確保范圍變量已更新和綁定

[英]How to ensure that a scope variable updates and binds before going to view angularjs

在我的代碼中,有一個名為$ scope.detailsPresent的范圍變量,它基本上只是檢查是否有數據並根據結果顯示不同的頁面。

發生的是,當我執行console.log($ scope.detailsPresent)時,該值是正確的,取決於是否存在該值應為false的數據,或者如果沒有數據則該值為true。

但是視圖尚未綁定值,因此在視圖上該值尚未更新,因此它未顯示正確的頁面。 如何確保在視圖中更新值? 我試過$ scope。$ apply(),但是出現錯誤,所以有辦法嗎?

my_controller.js

angular.module('my.controllers')
.controller('ReferralCtrl', function($scope, $rootScope, $window, $state, $q, $timeout, referralCasesGroupByCaseStatus, AuthenticationService, $ionicLoading) {

$scope.detailsPresent = {myVar: false};

$scope.showCaseStatusFromDashboard = function(number) { 
    $timeout(function() {
      $scope.$applyAsync(function() {
        $rootScope.fromDashboard = true; 
      })
    }, 1000, true);
      $scope.showCaseStatus(number); 
  }

 $scope.showCaseStatus = function(number) {

    if(changedNumber !== 0 && changedNumber !== number) {
      changedNumber = number;
    }
    else {
      if(changedNumber > 0) {
        $scope.$applyAsync($scope.detailsPresent.myVar = true);
      }
    }

    $timeout(function() {
      $scope.$applyAsync(function() {  
        referralCasesGroupByCaseStatus.showCaseStatus(number, $rootScope.listingDetails).then(function(listingCaseStatus) {
          $rootScope.listingByCaseStatus = angular.copy(listingCaseStatus);

          if(listingCaseStatus == 0 || listingCaseStatus == undefined || listingCaseStatus == null) {

            $scope.detailsPresent.myVar = true;  
            $scope.changeNumber = true; 
            $state.go('app.case_status') 
          }  
          else {
            $scope.detailsPresent.myVar = false;
            $scope.noMoreItemsAvailable = false; 
            $scope.changeNumber = true; 
            $state.go('app.case_status') 
          } 
        })
      })
    }, 1000);
  }

my.html

<ion-view view-title="Case Status">
<ion-content>
    <div ng-if="detailsPresent.myVar">
      <ng-include src="template='no-listings'"></ng-include>
    </div>
    <div ng-if="!detailsPresent.myVar">
      <ng-include src="'templates/case-listings.html'"></ng-include>
    </div>
  </ion-content>
</ion-view>

我已經參加了大約6天,但看不到成功。 任何幫助深表感謝。

這不是一個完整的答案,但是我有一些建議。 作為注釋,這樣做會更好,但是我還沒有足夠的聲譽。

嘗試將ng-if屬性切換為ng-show屬性。 我意識到這會將包含的元素留在DOM中,但是如果性能未受到嚴重影響,它可能對您有用。

另外,調用$scope.$apply()時出錯的原因是,您已經在調用$apply()$applyAsync()

有關$ apply()的更多詳細信息,請參見此答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM