【问题标题】:Load view after data is loaded加载数据后加载视图
【发布时间】:2014-09-11 12:50:59
【问题描述】:

我遇到了一些麻烦。我正在使用这个插件“angular-masonry”(它在 Github 上)在页面上动态构建网格。当页面加载我得到这个: http://joxi.ru/YBQPVP3JTJCwfIgLgbc

这是我的代码:

    <div class="container" style="width:80%">
      <h1 style="text-align: center; margin-bottom: 40px">
        Category: {{category.text}}
      </h1>
      <div>(masonry='' load-images="false")
        <div class="masonry-brick" ng-repeat="portal in category.claim_portals" style='width:50%;float:left'>
          <div>
            <h3>(style='margin-left:30px')
              Portal: {{portal.text}}
            </h3>
            <div class="category-list" ng-repeat="claim in portal.portal_claim" style="margin-bottom:2px">
              <div class="claim_sections">
                <claimforlist claim="claim"></claimforlist>
              </div>
            </div>
          </div>
        </div>
      </div>
</div>

但是在调整浏览器窗口大小后,一切都变得正常了,看起来像这样: http://joxi.ru/iBQPVP3JTJCUfLnoqQQ

我认为视图在 JSON 数据到达之前加载。 谁能帮助并告诉我如何在数据到达后加载视图?或者如果您知道此类问题的其他原因,请回复。

提前致谢。

【问题讨论】:

  • 你能创建一个 jsfiddle 吗?
  • 你可以添加 ng-if 像
  • 是的,我想了想加了,但是不行。

标签: json angularjs angularjs-directive masonry


【解决方案1】:

您可以添加一个值设置为false 的范围布尔变量,并在您的 http 承诺成功时将值更改为 true。

代码示例:

function myController($scope, YourDataServer) {
    $scope.dataLoadedSuccessfully = false;

    yourDataServer
        .query()
        .$promise
        .then(
            function(result) {
                $scope.dataLoaded = true; // set the value to true
            });
}

HTML 看起来像:

<div id="loadingBar" ng-show="!dataLoadedSuccessfully">Loading data...</div>

<div id="dataWrapper" ng-show="dataLoadedSuccessfully">
    <!-- data goes here -->
</div>

【讨论】:

  • 这是个好主意,但我解决了我的问题,在控制器中添加 $timeout/
猜你喜欢
相关资源
最近更新 更多
热门标签