Need help with service

Hello guys. I am still new using ionic and this is probably an easy to fix problem but here it goes.

For my app ive been having a few problems i cant seem to figure out why.
I’m trying to make some divs with the ng-repeat, and its been working great with dummy data in my controller.

but now when i want to get my data from my database, using json and $http its not working as its suposed. shows the ‘wrong’ unless i go re-enter the view.
i can see from the output that it just takes a bit time to get the data from the database should i then just set a timer for the Situation.getSituations() or is there a smarter way?.

also i’ve tried using callback at the update part, so that when i run my Situations.getSituations(), then it updates first and then returns the array, but from the output inside the service it looks perfectly, but the output in my controller is still wrong. and the data is not shown.

controller.js:

Situation.update();
$scope.$on('$ionicView.enter', function (e) {
    $scope.situations = Situation.getSituations();
    console.log($scope.situations);
});

services.js:

.factory('Situation', function ($http) {
    var situations = ["wrong"];
    var update = function () {
        var req = {
            method: 'GET',
            url: urlGET + "type=situations&userId=3",
            headers: {
                'Content-Type': "application/x-www-form-urlencoded"
            },
            data: "type=students"
        }
        $http(req).success(function (response) {
            situations = response.situations;
            console.log("updated! " + situations);
        }).error(function () {
            console.log('Der er sket kritisk fejl!, kontakt Actura');
        });
    }
    return {
        update: update,
        getSituations: function () {
            console.log("getSituations: " + situations)
            return situations;
        }
    }
})

console output:

Ionic Core: init
ionic.io.bundle.min.js:2 Ionic Core: searching for cordova.js
ionic.io.bundle.min.js:2 Ionic Core: attempting to mock plugins
services.js:25 getSituations: wrong
controllers.js:97 ["wrong"]
ionic.io.bundle.min.js:2 Ionic Push: register
services.js:16 updated! [object Object]
ionic.io.bundle.min.js:2 Ionic Push (dev): registered with development push service
ionic.io.bundle.min.js:2 Ionic Push: (debug) device token registered: <PushToken ['DEV-b4bb631f-7954-4c7a-9640-136cb662a216']>
app.js:30 Device token: DEV-b4bb631f-7954-4c7a-9640-136cb662a216
ionic.io.bundle.min.js:2 Ionic Push (dev): watching for new notifications

You need to use promises

Situation.getSituations().then(function(result){
    $scope.situations = result;
}

and getSituations should look like this:

   getSituations: function () {
            return $http(req)
        }
1 Like

All $http calls in angular return promises by default. Please read about Javascript promises and $q libary

https://docs.angularjs.org/api/ng/service/$q

2 Likes

Thank you very much, i’ll be adding that to my list things to read.

Just noticed my view is not scrollable, or atleast not while testing in browser using the ionic serve
is that due to its in browser and not android am i just missing some setting?

<ion-view view-title="Overblik">
<ion-content scroll="true" class="padding">
    <div class="list card" ng-repeat="situation in situations">
        <a href="#/tab/people/{{situation.contact.id}}">
            <div class="item item-divider {{situation.status}} ">Alarm {{situation.user.name}} - <small>{{situation.created}}</small></div>
            <div class="item item-body">
                <div>
                    Der er blevet sendt en besked til {{situation.contact.name}}</b><br />
                    <span class="icon ion-location"> Adresse </span>:  <b>{{situation.latLng}}</b>
                </div>
            </div>
        </a>
    </div>
</ion-content>

your code looks fine, it should scroll. Are you sure you are closing </ion-view>

yes, sorry for not having that part with.
Just tested it on a device and its working there. so its with the ionic serve that i have no scroll…
Thanks for all your help

It should work in browser as well. Atleast it does for me…

Just testet in another brower.

it works for me at
microsoft egde, internet explorer, firefox and android devices. but not at my google chrome.

Try click+drag instead of scroll