AngularJS:加载时如何防止页面中出现“代码闪烁”

塞姆雷

我已经使用AngularJS创建了一个简单的应用程序。当我打开页面一秒钟时,会看到以下屏幕:

在此处输入图片说明

但是,加载完成后,我会看到已加载和样式化的内容,这很好:

在此处输入图片说明

如何防止页面上闪烁AngularJS代码?这和FOUC有关吗?

这是HTML代码:

<!doctype html>
<html class="no-js" lang="en" ng-app="MainApp">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Foundation | Welcome</title>
    <link rel="stylesheet" href="css/foundation.css" />
    <script src="js/vendor/modernizr.js"></script>

    <style>
    .row.full-width {
        width: 100%;
        margin-left: auto;
        margin-right: auto;
        max-width: initial;
    }
    </style>

</head>

<body ng-controller="MainCtrl">
    <div class="off-canvas-wrap" data-offcanvas>
        <div class="inner-wrap">
            <nav class="tab-bar">
                <section class="right-small">
                    <a class="right-off-canvas-toggle menu-icon" href="#"><span></span></a>
                </section>
                <section class="left tab-bar-section">
                    <h1 class="title">Salary Calculator</h1>
                </section>
            </nav>

            <aside class="right-off-canvas-menu">
                <ul class="off-canvas-list">
                    <li>
                        <label>Location</label>
                    </li>
                    <li><a href="#">United Kingdom</a>
                    </li>
                </ul>
            </aside>

            <section class="main-section">


                <div class="row full-width">
                    <div class="large-4 columns">

                        <ul class="tabs" data-tab>
                            <li class="tab-title active"><a href="#panel1">Annual Salary</a>
                            </li>
                            <li class="tab-title"><a href="#panel2">Monthly Expenses</a>
                            </li>
                        </ul>
                        <div class="tabs-content">
                            <div class="content active" id="panel1">
                                <div class="row">
                                    <div class="large-12 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Salary" ng-model="salary"/>
                                    </div>
                                </div>
                            </div>
                            <div class="content" id="panel2">
                                <div class="row">
                                    <div class="large-4 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Rent" ng-model="rent" />
                                    </div>
                                    <div class="large-4 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Wireless, TV, Home Phone" ng-model="telecom"/>
                                    </div>

                                    <div class="large-4 columns">
                                        <input ng-change="calculate()" type="text" placeholder="TV License" ng-model="tv" />
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="large-4 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Mobile Phone" ng-model="mobile"/>
                                    </div>
                                    <div class="large-4 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Subscription" ng-model="subscription"/>
                                    </div>

                                    <div class="large-4 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Electricty" ng-model="electricity" />
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="large-4 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Food" ng-model="food"/>
                                    </div>
                                    <div class="large-4 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Transport" ng-model="transport" />
                                    </div>

                                    <div class="large-4 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Charity" ng-model="charity"/>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="large-12 columns">
                                        <input ng-change="calculate()" type="text" placeholder="Other" ng-model="other"/>
                                    </div>
                                </div>

                            </div>
                        </div>

                    </div>
                    <div class="large-8 columns"  ng-cloak >
                        <table >
                       <thead>
                            <tr>
                                <th width="200"></th>
                                <th width="250">Yearly</th>
                                <th width="250">Monthly</th>
                                <th width="250">Weekly</th>
                                <th width="250">Daily</th>
                            </tr>
                        </thead>
                        <tbody ng-repeat="val in results">
                            <tr>
                                <td>{{val.rowType}}</td>
                                <td>{{val.yearly}}</td>
                                <td>{{val.monthly}}</td>
                                <td>{{val.weekly}}</td>
                                <td>{{val.daily}}</td>

                            </tr>
                        </tbody>
                        </table>

                    </div>
                </div>



            </section>

            <a class="exit-off-canvas"></a>

        </div>
    </div>

    <script src="../bower_components/angularjs/angular.js"></script>


    <script src="js/app-service.js"></script>
    <script src="js/app-controller.js"></script>
    <script src="js/app-directives.js"></script>
    <script src="js/app.js"></script>

    <script src="js/vendor/jquery.js"></script>
    <script src="js/foundation.min.js"></script>

    <script>
    $(document).foundation();
    </script>
</body>

</html>

编辑:

除已接受的解决方案外,还请参阅我的答案以寻求替代解决方案。

米克尔

ng-cloak会在一定程度上有所帮助,但是您可以使用ng-bind指令(而不是)完全阻止它{{ }}

例如

<td ng-bind="val.monthly"> </td>

<td>{{val.monthly}}</td>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

防止由于JavaScript内容注入而导致页面在加载时闪烁

Angular 6防止[隐藏]在页面加载时闪烁

如何防止在实际数据加载之前在“ AngularJs应用程序”的UI中出现“ {{}}”句柄?

是否有可能防止php代码在页面加载时加载?

防止在页面上可见的 Chrome 扩展程序的每个页面加载时闪烁?

使用背景幕时如何防止模式出现在页面加载中:“静态”

如何防止在页面加载时显示页面元素

防止$ scope更新时AngularJs内容闪烁

如何防止页面加载时触发Onchange触发

如何防止故事板在页面加载时自动播放?

如何防止侧边栏在页面加载时瞬间显示?

React-admin:如何在页面加载时防止 getAll

每次加载页面时如何加载php代码?

如何防止Excel中出现屏幕失真

如何防止数组中出现重复项?

jQuery / Select2 width = 100%在加载时创建“闪烁”,如何防止?

如何在加载时延迟显示WinForm以防止其控件闪烁?

如何避免在Leaflet Tile WMS实现中出现闪烁?

如何避免在Shiny R图中出现闪烁错误?

使用* ngIf时如何防止闪烁?

如何防止在执行代码时弹出框出现?

Chrome中出现警报后如何重新加载javascript中的页面

如何防止在C中出现段错误(当接受错误类型的参数时)

更新项目时如何防止mongoDB中出现重复项目

模态打开时,如何防止Enter键在文档中出现keydown事件?

刷新页面时,如何避免部署的 React 应用程序中出现 404 错误?

如何加载以在页面加载时显示Bootstrap选项卡而不先闪烁第一个选项卡?

在 Angularjs 中加载页面时如何执行函数

防止表单提交时重新加载页面