简体   繁体   中英

Is there an alternative to ng-init to execute all the times?

First of all. I'm sorry if my explanation is confused. If you see the code below which is my index.html from my Ionic project. It executes ok in the first time, but as soon as I add a new word and in the ng-repeat it has more than 1 word.. the function inside the ng-init execute just one time, just for the last word (string) added. Not for all again.

So, the ng-init is executing just one time... not for all the words. Is there any alternative for 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>

This is my 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)

   });

}

First of all you shouldn't apply this code in your controller but in a service. You could do something like:

Service

.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
    }
})

And in your controller

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM