I like to implement a search against a http resource in angular js. Evertything is fine as long as I search from any other page than the result page. If i search at the result page again I get the result Objects but the scope would not update. You can find an example of this behaviour here: Example page.
Steps to reproduce:
- Enter "pax6" into the search field in the menu bar. Recognize you get forwarded to the result page.
- Staying on the search page, enter pax8 into the search field. You will receive the objects but the scope won't update.
- Click on any other link than search
- Click on search again, now the view shows the correct values
Here is how I implemented the feature:
angular.module('gp3').controller('SearchController',function($scope,$location,SearchService,$http) {
$scope.keyword="";
$scope.options={};
$scope.results = SearchService.getResults();
$scope.search = function() {
SearchService.search($scope.keyword,$scope.options).then(function(data) {
$scope.results = data;
console.log(data);
$location.path('/search');
},function(reason) {
$scope.results = [];
$location.path('/search');
});
};})
.factory('SearchService',function($http,$q) {
var SearchService = {};
var results = [];
SearchService.getResults = function() {
return SearchService.results;
};
SearchService.search = function(keyword,options,callback) {
var def = $q.defer();
$http.get("/api/search/" + keyword,options)
.success(function(data,status,headers,response){
SearchService.results = data;
def.resolve(data);
})
.error(function(status,headers,response){
def.reject("Failed to search");
})
return def.promise;
};
return SearchService;})
I looked arround here at stackoverflow alot but I only can find solutions where the async (search) function is called when the controller loads and not with click on a button. If I missed any Information or the question is not well formatted please tell me, this is my first question I am asking. Thank you.
