Ionic view transition flickering issue

2019-07-30 00:14发布

问题:

I call API and return the response for binding in $ionicView.beforeEnter. However, when the view is entered, sometimes I got the view loaded but the data is not being bind until few seconds later, especially in a bad network connectivity. How could this issue be solved?

Would this because of my API call is asynchronous?

angular.module('app').controller(function($scope, ApiMethod) {
    $scope.$on("$ionicView.beforeEnter", function(event, data){
       ApiMethod.GetFormInfo().$promise.then(function (res) {
            $scope.res = res;
       }, function (errRes) {

       })
    });
});

<ion-content>
    <form name="form">
        <div class="list">
          <label class="item item-input">
            <input type="text" placeholder="First Name" ng-model="res.firstName">
          </label>
          <label class="item item-input">
            <input type="text" placeholder="Last Name" ng-model="res.lastName">
          </label>
          <label class="item item-input">
            <textarea placeholder="Comments"  ng-model="res.comments"></textarea>
          </label>
        </div>

        <button type="submit">Submit</button>
    </form>
</ion-content>

回答1:

Yes, there is a flicker because of the pause asynchronous ApiMethod call. To gracefully handle this inside my Ionic application I use $ionicLoading. This will "freeze" the user from moving around until the data has been loaded. I believe it is better to notify them of the loading then make them question a blank view.

Implemented:

angular.module('app').controller(function($scope, ApiMethod, $ionicLoading) {

    $scope.show = function() {
        $ionicLoading.show({
            template: 'Loading...'
        }).then(function(){
            console.log("The loading indicator is now displayed");
        });
    };
    $scope.hide = function(){
        $ionicLoading.hide().then(function(){
            console.log("The loading indicator is now hidden");
        });
    };

    $scope.$on("$ionicView.beforeEnter", function(event, data){
        // Show loading
        $scope.show();
        ApiMethod.GetFormInfo().$promise.then(function (res) {
            $scope.res = res;
            // Hide loading
            $scope.hide();
       }, function (errRes) {

       })
    });
});

Reference: $ionicLoading