是否有替代ng-init来执行所有时间?

时间:2016-08-11 16:53:12

标签: javascript angularjs ionic-framework

首先。如果我的解释很混乱,我很抱歉。如果您从我的Ionic项目中看到下面的代码是我的index.html。它在第一次执行ok,但是只要我添加一个新单词并且在ng-repeat中它有超过1个单词.. ng-init中的函数只执行一次,只是为了最后一个单词(string ) 添加。不是全部。

所以,ng-init只执行一次......不是所有的单词。有没有替代ng-init?

<div class="buttons">
    <button ng-click="vm.showAddBirthdayModal()" class="button button-icon icon ion-android-add">
    </button>
</div>

<ion-item ng-repeat="b in vm.birthdays" ng-click="vm.showEditBirthdayModal(b)">
    <div style="float: left">
        <ion-text>{{b.Name}} : </ion-text>
        <ion-text ng-init="getTwitter(this.b.Name)">{{twitts[b.Name].count}}</ion-text>                 
    </div>
</ion-item>

这是我的getTwitter:

$scope.getTwitter = function(hashtag) {  



   $scope.twitts = {};

   var posOptions = {timeout: 10000, enableHighAccuracy: false};

   $cordovaGeolocation.getCurrentPosition(posOptions)   
   .then(function (position) {

      var lat  = position.coords.latitude;
      $scope.lat = lat;

      var long = position.coords.longitude;
      $scope.long = long;

        $ionicLoading.show();

        $http.get('https://twitter.net/api/twitter/count?lat=' + lat + '&lng=' + long + '&keywords=' + hashtag + '')
        .success(function(twitts) {
            $scope.twitts[hashtag] = twitts;
            //console.log('Twitts: '+$scope.twitts);
            $ionicLoading.hide();
        })
        .error(function(erro) {
            //$scope.twitts = twitts;
            console.log(erro);
        })
        .finally(function() {
        // Stop the ion-refresher from spinning
        $scope.$broadcast('scroll.refreshComplete');
        });

        console.log('Latitude: '+ lat + ' Longitude: ' + long)

   }, function(err) {

      console.log(err)

   });

}

1 个答案:

答案 0 :(得分:0)

首先,您不应在控制器中应用此代码,而应在服务中应用此代码。你可以这样做:

服务

.service('tweetSrv', function ($q, $http, $cordovaGeolocation, $ionicLoading) {

    var getTweets = function (twitts) {
        var posOptions = {
            timeout: 10000,
            enableHighAccuracy: false
        };


        return $q(function (resolve, reject) {

            $cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {

                var lat = position.coords.latitude;
                //                $scope.lat = lat;

                var long = position.coords.longitude;
                //                $scope.long = long;

                $ionicLoading.show();

                $http.get('https://twitter.net/api/twitter/count?lat=' + lat + '&lng=' + long + '&keywords=' + hashtag + '')
                    .then(function (response) {
                        resolve(response);
                        //console.log('Twitts: '+$scope.twitts);
                        $ionicLoading.hide();

                    }, function () {
                        reject("Something's wrong");
                    })

                console.log('Latitude: ' + lat + ' Longitude: ' + long);

            }, function (err) {

                console.log(err)

            })

        })
    }

    return {
        getTweets: getTweets
    }
})

在您的控制器中

var birthdaysLength = $scope.birthdays.length();
var i = 0;

var getBirthdaysTweets = function (name, index) {
    tweetSrv.getTweets(name).then(function (tweetsCount) {
        $scope.birthdays[index].tweets = tweetsCount;
        index++;
        if (index < birthdaysLength)
            getBirthdaysTweets($scope.birthdays[index].name, index);
        else {
            $scope.$broadcast('scroll.refreshComplete');
            console.log("Done. I've loaded tweets for all birthdays");
        }
    }, function (errorString) {
        console.log(errorString);
    })
}

getBirthdaysTweets($scope.birthdays[i].name, i);