Hey,
I’m stuck with this for two days now and can’t wrap around the problem. I first try to show what I have so far and at the bottom of the post I describe my problem:
What I have:
I have two states: (1) a state with a simple form that allows for geodata input and (2) a state that retrieves a list of post from a server.
/* startseite, auswahl ort */
.state('tab.suche', {
url: '/suche',
views: {
'tab-unterkuenfte': {
templateUrl: 'templates/tab-suche.html',
controller: 'SearchHomeCtrl'
}
}
})
/* merkliste / favoriten */
.state('tab.ergebnisse', {
url: '/unterkuenfte',
views: {
'tab-ergebnisse': {
templateUrl: 'templates/tab-ergebnisse.html',
controller: 'ErgebnisseCtrl'
}
}
})
I have a service that uses stored vars to do an ajax call
.service('nubiiSearch', function($http) {
searchVars = {
location_name : null,
location_lat : null,
location_lng : null,
checkin : null,
checkout : null,
};
posts = [];
this.getSearchVars = function() {
return searchVars;
};
this.setSearchVar = function(property, value) {
searchVars[property] = value;
}
this.getPosts = function() {
return $http({
method: 'GET',
url: 'http://localhost/nubii2/wp-admin/admin-ajax.php',
params: {
action: 'nubii_app_get_objects',
location_lat: searchVars.location_lat,
location_lng: searchVars.location_lng,
checkin: searchVars.checkin,
checkout: searchVars.checkout,
},
}).then(function(response){
posts = response.data;
return posts;
});
}
})
Then for each state I have my controller. For the first state the controller takes the form input and stores it in my service. When user hits the search button he’s redirected to second state that shows the results
.controller('SearchHomeCtrl', function($scope, $state, nubiiSearch) {
$scope.search = {};
$scope.autocompleteOptions = {
componentRestrictions: { country: 'de' },
}
$scope.searchInit = function() {
nubiiSearch.setSearchVar('location_name', $scope.search.location.formatted_address);
nubiiSearch.setSearchVar('location_lat', $scope.search.location.geometry.location.A);
nubiiSearch.setSearchVar('location_lng', $scope.search.location.geometry.location.F);
$state.go('tab.ergebnisse');
}
/*
* geolocation – get current lat and lng
*/
$scope.getCurrentLocation = function() {
ionic.Platform.ready(function() {
var position = navigator.geolocation.getCurrentPosition(function(position) {
$scope.search.location.lat = position.coords.latitude;
$scope.search.location.lng = position.coords.longitude;
});
});
};
})
My controller for the second state uses my service to fetch the data from the server and store it in §scope.posts
.controller('ErgebnisseCtrl', function($scope, $stateParams, nubiiSearch) {
/*
* get posts
*/
nubiiSearch.getPosts().then(function(posts){
$scope.posts = posts;
},function(error){
//Something went wrong!
});
})
in my template for second state I iterate through scope with ng-repeat likes this
<ion-list class="list">
<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="post in posts" type="item-text-wrap" href="#/tab/unterkuenfte/{{post.ID}}">
<h2>{{post.title}}</h2>
<p>{{post.ID}}</p>
</ion-item>
</ion-list>
The problem:
This works fine when I enter a location in the first state. The results are properly displayed on the second state. But when I go back to the first state and change the location, the second state doesn’t get updated. I think I miss some very basic thing here… the getPosts function in my service gets called when I updated the form input and the result get fetched (I can console.log it). But the $scope.posts doesn’t get update and so my view stays the same. Any help would be appreciated.
Thanks,
Florian