angular.js - How to solve the problem of angularjs displaying placeholders due to slow network speed?
ringa_lee
ringa_lee 2017-05-15 16:50:36
0
4
1062

I believe everyone will encounter this problem when writing angularjs, but it is not obvious in general web. When writing a wap page, it will be obvious that there is a placeholder that will stop when the interface is just opened, and then wait for the js to be asynchronous. After refreshing, the value changes again, and the experience is not very good.

I think there should be a more mature solution for this, please give me some advice~!

ringa_lee
ringa_lee

ringa_lee

reply all(4)
我想大声告诉你

@Broooooklyn Following this hint, I found a very good post on stackoverflow with a very clear explanation

PHPzhong

ng-bind or ng-cloak, the official has said it many times, using css to solve it is the best and perfect

http://www.cnblogs.com/whitewolf/p/3495822.html

https://docs.angularjs.org/api/ng/directive/ngCloak

[ng:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
display: none !important;
}

洪涛

Please use ng-bind

某草草

angularjs will automatically cooperate with $qProvider, which is actually the underlying $digest loop.

Option 1

When the reject or resolve of $q's promise is not executed, the attempt will not be rendered, let alone the problem of primary placeholder. In the router module of angularjs, you can use this function by defining resolve. angular-ui-routerUse the same principle.

Option 2

Don’t use {{ }} placeholders, use the ng-bind directive. But this solution is not flexible.
For example, using {{ }}, you can write it like this

html<p>{{ start_time }} ~ {{ end_time }}</p>

But that’s all you can do after using the ng-bind command.

<p>
    <span ng-bind="start_time"></span>
    ~
    <span ng-bind="start_time"></span>
</p>

A lot of useless empty tags will appear. Not conducive to maintenance. And the page will appear blank.

It is recommended to use the first option. But the first one also has a shortcoming, that is, when the data is returned slowly, the view returns to the whiteboard, but it is still sent. You can use ng-animate and add loading animation when switching pages.

router.js router.js angular-ui-router The module sample code is as follows

javascript.state('admin.event', {
    url: '/events',
    views: {
        'MainBody': {
            // 重点在这里
            templateUrl: '.....',
            controller: 'dzadmin.controller.events.query', //这里需要制定控制器名称,视图里不要在再制定了。因为是有 路由器 负责告诉控制器合适开始执行,而不是试图来驱动。
            resolve: {
                events: ['eventService', '$q', function(eventService, $q){
                    var deferred = $q.defer();
                    eventService.query(.....).then(deferred.resolve, deferred.reject);
                    return deferred.promise;
                }]
            }
        }
    }
})

// dzadmin.controller.events.query

.controller('dzadmin.controller.events.query', ['$scope', 'events', function($scope, events){
    $scope.events = events;
}])

// Try the code
一定要注意,视图里面不能再重复指定控制器名称。否则会报错, it should be that the controller name has been specified in the route.

<ul>
 <li ng-repeat="event in events">{{ event.name }}</li>
</ul>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template