Search code examples
angularjsangularjs-scopereferenceerror

Reference error when searching on submit using AngularJS


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.


Solution

  • 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) { .....