1

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

HTML:

<div ng-controller="search_interest" layout="column">
  <md-content class="md-padding" layout="column">

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

  </div>

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

2
  • echo json_encode($row_set); is enough. Commented Jul 23, 2015 at 10:27
  • Yes i know is enough, but let me explain that i used as shown in my code above because else i get results {"name":"Broccoli","type":"Brassica"} in console (so without [ ]). it was a try. But also echo json_encode($row_set); autocomplete not works Commented Jul 23, 2015 at 10:41

1 Answer 1

2

Ok. Just resolved my issue using promise. In controller i declared $q

function($scope, $http, $q)
{
    $scope.searchText = '';
    $scope.selectedItem = undefined;
    $scope.selectedInterest = [];

    var deferred = $q.defer();

In function i used $q:

$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));
          alert(results.name);
          deferred.resolve(results);
        });
        return deferred.promise;
    };

Thanks!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.