Angularjs autocomplete json with and without http

In my code i use angular (material) autocomplete and i need to get data from external php file.

HTML:

  <md-autocomplete 
    md-selected-item="selectedItem" 
    md-search-text="searchText" 
    md-items="item in getInterest1(searchText)" 
    md-item-text="item.name" 
    placeholder="Search for a vegetable">
    <span md-highlight-text="searchText">{{item.name}} :: {{item.type}}</span>
  </md-autocomplete>
When i return an "handmade" JSON, autocomplete works well:
//RETURN: results [{"name":"Broccoli","type":"Brassica"}]
    $scope.getInterest1 = function() {
      var results = [
        {
          'name': 'Broccoli',
          'type': 'Brassica'
        }];
        console.log('results', JSON.stringify(results));
        return results;
        };

But when i call the same JSON using $http.get it doesn’t show results in autocomplete and nothing happends.

//RETURN: results [{"name":"Broccoli","type":"Brassica"}]
    $scope.getInterest2 = function() {
      $http.get("../inc/users/search_interest.php?query=" + $scope.searchText)
        .success(function(results) {
          //$scope.interest = results;
          console.log('results', JSON.stringify(results));
          return results.data;
        });
    };

But i can see in console that results are equals.

In my php file (which is called by $http.get) i return JSON with json-encode:

$row_set = array('name' => 'Broccoli', 'type'=> 'Brassica');
echo json_encode(array($row_set));

Maybe php json_encode return a bad formed json?

Thanks

You need to add the return keyword before your $http call, and I think you might need to use then() rather than success():

$scope.getInterest2 = function() {
    return $http.get("../inc/users/search_interest.php?query=" + $scope.searchText)
        .then(function(results) { return results.data; });
};
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.