0

I am having issues with search on submit using angularJS

Here is html, contained insied module and controller,

<form ng-submit="search()">
            <div class="input-group col-md-6 typeahead-wrapper">
                <input type="text" ng-model="searchkeyword.keystring" id="searchField" placeholder="Type any keyword to search for a Masjid" class="form-control typeahead">
              <span class="input-group-btn">
                  <button class="btn btn-primary" id="goSearch" type="submit"><span class="glyphicon glyphicon-search"></span></button>
              </span>
            </div><!-- /input-group -->
            </form>

Search function:

app.controller('MyCtrl', ['$scope', '$http', function($scope, $http) {
$scope.searchkeyword    = {};
$scope.results = [];
$scope.searchkeyword.bylocation=2;
$scope.searchkeyword.lat=-1;
$scope.searchkeyword.lon=-1;
$scope.search = function() {
  /* the $http service allows you to make arbitrary ajax requests.
   * in this case you might also consider using angular-resource and setting up a
   * User $resource. */
  $http.get('http://localhost/json/search.php', { params: searchkeyword },
    function(response) { $scope.results = response; alert(response); },
    function(failure) { console.log("failed :(", failure); });
}}]);

As I submit, it gives the error

ReferenceError: searchkeyword is not defined

Thanks for help.

4
  • 1
    $scope.searchkeyword should be in http.get Commented Sep 8, 2014 at 5:59
  • Thanks, removed that error, but not going into response/failure functions. Commented Sep 8, 2014 at 6:03
  • 1
    $http.get('localhost/json/search.php?params='+$scope.searchkeyword)..... Commented Sep 8, 2014 at 6:13
  • 1
    You should not pass callbacks to http.get, it returns a promis so you have to use http.get().then(sucesscalback,errorcalback) Commented Sep 8, 2014 at 6:18

2 Answers 2

1

try with $scope.searchkeyword you are using only the searchkeyword

var url = 'http://localhost/json/search.php?params='+$scope.searchkeyword;
$http.get(url).function(response) { .....
Sign up to request clarification or add additional context in comments.

2 Comments

Error: response is not defined.
Done it the sameway, but with callbacks as @MiTa suggested, thanks.
1

change below code

        $http.get('http://localhost/json/search.php', { params: searchkeyword },

to

       $http.get('http://localhost/json/search.php', { params: $scope.searchkeyword },

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.