简体   繁体   English

如何在查看angularjs之前确保范围变量已更新和绑定

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

In my code, there is this scope variable called $scope.detailsPresent which basically just checks if there is data and displays a different page based on the result. 在我的代码中,有一个名为$ scope.detailsPresent的范围变量,它基本上只是检查是否有数据并根据结果显示不同的页面。

What happens is that when i do console.log($scope.detailsPresent) the value is correct based on if there is data which the value should be false or if there is no data the value should be true. 发生的是,当我执行console.log($ scope.detailsPresent)时,该值是正确的,取决于是否存在该值应为false的数据,或者如果没有数据则该值为true。

But the view doesnt bind the value yet so on the view the value is not yet updated hence it doesnt show the correct page. 但是视图尚未绑定值,因此在视图上该值尚未更新,因此它未显示正确的页面。 How do i ensure that the value is updated in the view ? 如何确保在视图中更新值? I have tries $scope.$apply() but i get an error so is there anyway to do it? 我试过$ scope。$ apply(),但是出现错误,所以有办法吗?

my_controller.js 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 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>

I have been on this for about 6days but no success in sight. 我已经参加了大约6天,但看不到成功。 Any help is deeply appreciated. 任何帮助深表感谢。

This isn't a full answer, but I have some suggestions. 这不是一个完整的答案,但是我有一些建议。 This would work better as a comment, but I don't have enough reputation yet. 作为注释,这样做会更好,但是我还没有足够的声誉。

Try switching your ng-if attributes to ng-show attributes. 尝试将ng-if属性切换为ng-show属性。 I realize this leaves the included elements in the DOM, but it might work for you if the performance is not seriously affected. 我意识到这会将包含的元素留在DOM中,但是如果性能未受到严重影响,它可能对您有用。

Also, the reason you get an error when calling $scope.$apply() is because you're already within a call to $apply() , or rather $applyAsync() . 另外,调用$scope.$apply()时出错的原因是,您已经在调用$apply()$applyAsync()

See this answer for more details on $apply() 有关$ apply()的更多详细信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM