【问题标题】:AngularJS - Loading icon whilst waiting for data/data calculationAngularJS - 在等待数据/数据计算时加载图标
【发布时间】:2014-05-06 09:16:36
【问题描述】:

我有一个简单的 Angular http.get:

app.factory('countriesService', function($http) {
return {
    getCountryData: function(done) {
        $http.get('/resources/json/countries.json')
        .success(function(data) { done(data);})
        .error(function(error) {
            alert('An error occured');
        });
    }
}
});

示例 .JSON:

{
"NoCities": 66,
"Balance": 2103,
"Population":  63705000,
"CityInfo": [
    {
        "CityName": "London",
        "CityPopulation": "7825200",
        "Facts": {
            "SubFact1": "xzy",
            "SubFact2": "xzy",
            "SubFact3": "xzy",
            "SubFact4": "xzy",
            "SubFact5": "xzy"
        },
    },
    {
        "CityName": "Manchester",
        "CityPopulation": "2584241",
        "Facts": {
            "SubFact1": "abc",
            "SubFact2": "abc",
            "SubFact3": "abc",
            "SubFact4": "abc"
        },
    }

],
"SubmitInfo": null,
"FormAction": null,
"FormController": null,
}

我注意到当我的页面执行 .length 时,有时可能需要一段时间才能加载数据,例如:

<div>
    <a>Countries</a> : {{Countries.length}}
</div>

Angular 是否有某种形式的等待/加载图标,我可以在填充 DIV 中的数据时显示这些图标?

理想情况下是轻量级的,不需要加载库(我的应用程序也在使用 jQuery)

谢谢

【问题讨论】:

    标签: jquery angularjs get angularjs-directive angularjs-scope


    【解决方案1】:

    这是我通常的做法。就 OZ_ 而言,这需要Font Awesome&lt;i&gt; 的类 fa fa-spinner fa-spin 是对该库的引用。

    不过,您也可以选择显示/隐藏表示正在加载的.gif

    标记

    使用ng-hideng-show 控制将包含您的填充数据的微调器和元素的可见性。

    <p class="text-center" ng-hide="dataLoaded">
        <i class="fa fa-spinner fa-spin"></i>
    </p>
    <div ng-show="dataLoaded">
        <a>Countries</a> : {{Countries.length}}
    </div>
    

    JS

    在打电话之前,将$scope.dataLoaded 设置为false。然后,在您的 success 块中,将其设置为 true。另外值得注意的是,您需要将$scope 传递给您的工厂。

    app.factory('countriesService', function($http, $scope) {
        return {
            getCountryData: function(done) {
                $scope.dataLoaded = false;
                $http.get('/resources/json/countries.json')
                .success(function(data) { 
                    done(data); 
                    $scope.dataLoaded = true;
                 })
                .error(function(error) {
                    alert('An error occured');
                });
            }
        }
    });
    

    【讨论】:

    【解决方案2】:

    AngularJS 不是 CSS 框架。您可以在 FontAwesome 的 TWBS 中找到加载图标: http://fortawesome.github.io/Font-Awesome/examples/#spinning

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多