javascript – 如何为Ionic中的每个图像加载添加微调器

我正在尝试加载每个图像加载时显示微调器的图像列表.

这是我所拥有的,但没有看到如何在加载每个图像时添加微调器,而不是在加载时看到空白屏幕

<div ng-repeat="i in data| filter: { tags: tag } " class='wrapper' id="homeImg">
    <!-- wrapper div -->

    <!-- image -->
    <a href="#/information"> <img class="ng-cloak" style="float:left; display:inline-block; overflow:hidden; border: 1px;" class="fill_image" src='{{ i.picture }}' style width="100%" style height="100%" ng-click="disableClick('{{ i.firebase_url }}')" /> </a>
    <!-- description div -->
    <div class='description'>
        <!-- description content -->
        <p class='description' style="float: left">
            {{i.title }}
        </p>
        <p class='description' style="float: right">
            {{i.location}}
        </p>

        <!-- end description content -->
    </div>
    <!-- end description div -->
</div>

最佳答案 您可以使用||轻松完成此操作ng-src标记内的运算符:

控制器:

$scope.loading = "<some-pic.gif>";

视图:

<!-- image -->
<a href="#/information">
    <img ng-src='{{ (i.picture) || (loading) }}'/>
</a>

>将src标签更改为ng-src,它更加Angular友好
>定义加载图像/ gif(上次上传)并将其存储在$scope变量中
>使用||运算符,如果第一个选项未定义,则显示第二个选项(gif)

小提琴:http://jsfiddle.net/xf3ezakc/

此外,您可以在控制器内部使用$ionicLoading来显示加载警报,直到所有图像都已加载为止,我回答了另一个关于如何执行此操作的问题here.

$scope.show = function() {
    $ionicLoading.show({
        template: 'Loading...'
    }).then(function(){
        console.log("The loading indicator is now displayed");
    });
};
$scope.hide = function(){
    $ionicLoading.hide().then(function(){
        console.log("The loading indicator is now hidden");
    });
};

// Assuming you have `$scope.data`, which it seems so
$scope.data = {};
$scope.show();
someDataCall().then(function(data) {
    // success
    $scope.hide();
}).catch(error) { 
    // error
    $scope.hide();
});

我看到你的代码中也有一个firebase引用,如果你使用$firebaseArray或$firebaseObject,你可以使用$ionicLoading结合$loaded来检测图像加载的时间(包含在AngularFire API中):

$scope.data.$loaded().then(function(data) {
    // success
    $scope.hide();
}).catch(function(error) {
    // error
    $scope.hide()
});

对于最后2个,请确保已注入$ionicLoading

参考文献:

ng-src

$ionicLoading

AngularFire

ion-spinner(我从未使用过它)

点赞