繁体   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