如何在离子应用中添加微调器?

时间:2017-04-01 09:55:20

标签: javascript angularjs ionic-framework

我已经制作了一个plunker,其中我从json api获取数据但是当我尝试在我的js代码中添加加载功能时,应用程序停止工作可以有人帮忙吗?

var app = angular.module('newsapp', ['ionic']);

app.controller('newscontroller',function($scope, $http){
    $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");
        });
    };

    $scope.news =[];
    $scope.show();

    $http({
        method: "GET",
        url: "https://newsapi.org/v1/articles?source=techcrunch&sortBy=latest&apiKey=90b77760d33d4c76af4c6c53862c9e72"
    }).then(function(newsdata){
        angular.forEach(newsdata.data.articles, function(newsarticle) {
            $scope.news.push(newsarticle);
        });
        $scope.lastarticleID = newsdata.data.lastID;
        $scope.hide();
        console.log(newsdata);
    })
});

1 个答案:

答案 0 :(得分:1)

您错过了将$ionicLoading引用解析为您的控制器。看一下这个running plnkr

var app = angular.module('newsapp', ['ionic']);

app.controller('newscontroller', function ($scope, $http, $ionicLoading, $timeout) {

    $scope.show = function () {
        $ionicLoading.show({
            template: '<img width="32" height="32" src="data:image/gif;base64,R0lGODlhHgAeAPf2AP7+/v39/fDw8O/v7/z8/PHx8e7u7vv7++Xl5fr6+vn5+ebm5gAAAPX19fT09Pb29vPz8/f39/j4+Ofn5/Ly8tTU1O3t7dXV1cnJyezs7Ojo6Orq6uTk5OPj476+vuvr69nZ2cjIyNbW1unp6crKytjY2MvLy9zc3LOzs7KyssfHx+Hh4b+/v9/f3+Li4tPT097e3sDAwNfX193d3dra2sHBwYmJidvb2+Dg4L29vby8vM/Pz7e3t9LS0sTExNDQ0LS0tIiIiLW1tcbGxszMzLi4uLq6uoyMjHBwcMPDw8XFxVhYWLGxsXFxccLCws7Ozra2trCwsG9vb42Njbm5uc3NzXNzc4qKilpaWtHR0bu7u3JycpKSkjs7O3Z2dq+vr66urj09PVl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AYNN9QwMMKBgwQQEJBVWgSWqCEkaseiZCUAgwYEGHG4GsBdhA44TCQg2+pbJTyQFZ0wk1ABBAQ4RFXogJTgA26Jev/pAhCDigowLGhISSLRGUw6IAU68uDAAYg46DzhuWHAQYUYQIZxwUHngwwcLEHLaS0CF06FajlB9UamARAgMJn7cEBDBjjFFYcKgEqRSAobnGEjs2CBQQo8oqdQQ0dmixQq+axFSxIhCgSOOFrIT1gthKg7IhxKU6DCRtSAAQ6HQVEqWMuEKLTXEkMQICLmBTCXFcDGACu8R1IAKBYxAggc5eGABQQjQUQYfqxWAixR2ZNBBCxp0wEMU2wUwwgUk/LDUQA4NlIIUSJxRwB1v8KEAFVCgcOFA6SFEwBVNfJLBA3hcYYg9N6SAggg62bOAF0iQwJYeQUBhDwAkRFFDeBwpcQ0LA+XxhgoCHaBCCvVBVIVeAzFRxgkEvTBUlARdkEubeCIUAZQqBQQAOw==">'
        }).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");
        });
    };

    $scope.news = [];
    $scope.show();

    $timeout(function () {
        $http({
            method: "GET",
            url: "https://newsapi.org/v1/articles?source=techcrunch&sortBy=latest&apiKey=90b77760d33d4c76af4c6c53862c9e72"
        }).then(function (newsdata) {
            angular.forEach(newsdata.data.articles, function (newsarticle) {
                $scope.news.push(newsarticle);
            });
            $scope.lastarticleID = newsdata.data.lastID;
            $scope.hide();
        })
    }, 2500);
});