Angularjs - Hide content until DOM loaded

后端 未结 6 2192
深忆病人
深忆病人 2020-12-01 02:17

I am having an issue in Angularjs where there is a flicker in my HTML before my data comes back from the server.

Here is a video demonstrating the issue: http://you

6条回答
  •  感动是毒
    2020-12-01 03:02

    Add the following to your CSS:

    [ng\:cloak],[ng-cloak],.ng-cloak{display:none !important}
    

    The compiling of your angular templates isn't happening fast enough.

    UPDATE

    You should not do DOM manipulation in your controller. There are two thing you can do... 1. You can intercept changes to the value within the scope of the controller via a directive! In your case, create a directive as an attribute that is assigned the property you want to watch. In your case, it would be caseData. If casedata is falsey, hide it. Otherwise, show it.

    1. A simpler way is just use ngShow='casedata'.

    Code

    var myApp = angular.module('myApp', []);
    myApp.controller("caseCtrl", function ($scope, $http, $routeParams, $timeout) {
        $scope.caseData = null;
        //mimic a delay in getting the data from $http
        $timeout(function () {
            $scope.caseData = 'hey!';
        }, 1000);
    
    })
        .directive('showHide', function () {
        return {
            link: function (scope, element, attributes, controller) {
                scope.$watch(attributes.showHide, function (v) {
                    if (v) {
                        element.show();
                    } else {
                        element.hide();
                    }
                });
            }
        };
    });
    

    HTML

    using directive
    using ngShow

    JSFIDDLE:http://jsfiddle.net/mac1175/zzwBS/

提交回复
热议问题