Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My question is very similar to this post 'Using typeahead and ajax in a AngularJS app'

Coffeescript:

  $scope.tradingPartners = (searchOn) ->
    console.log("Searching on #{searchOn}")
    $.getJSON("../tp/tpLookupAdmin", {term: searchOn, max: 20}, (response)->
        response)

Generates Javascript:

$scope.tradingPartners = function(searchOn) {
      console.log("Searching on " + searchOn);
      return $.getJSON("../tp/tpLookupAdmin", {
        term: searchOn,
        max: 20
      }, function(response) {
        return response;
      });
    };

Using it:

<input type="text" ng-model="testScript.sender" typeahead="sender as sender.label for sender in tradingPartners($viewValue)" 

So whats wrong? ...

The getJSON call is made just fine, the results look good but the typeahead does not do anything. If I put hardcoded values in as the return from the function it works just fine.

Now I know the getJSON is not just returning an object array, and doing

$.getJSON("../tp/tpLookupAdmin", {term: searchOn, max: 20}, (response)->
        response).responseJSON

gives undefined.

Example hardcoded json that works:

[{"id":"1","label":"test1"},{"id":"2","label":"test2"}]

I'm missing something simple here...

share|improve this question

1 Answer

The accepted answer to the post you are referencing in the beginning tells that the lookup function must return a (AngularjS-style) promise. $.getJSON does not return a promise and is not even a AngularJS-function. Instead of $.getJSON use the $http service provided by AngularJS like it is shown in that post.

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.