1

I've built a simple search form in AngularJS but get an undefined error.

The code is as follows for the list controller:

var phonecatControllers = angular.module('phonecatControllers', []);

phonecatControllers.controller('PhoneListCtrl', ['$scope', 'Phone',
  function($scope, Phone) {
    $scope.phones = Phone.query();
    $scope.orderProp = 'age';
  }]);

And the search controller as:

phonecatControllers.controller('SearchCtrl', ['$scope', 'Phone', 
    function ($scope, $http) {
        $scope.url = 'phones.json';
        // The function that will be executed on button click (ng-click="search()")
        $scope.search = function() {

            // Create the http post request
            // the data holds the keywords
            // The request is a JSON request.
            $http.post($scope.url, { "data" : $scope.keywords})
            .success(function(data, status) {
                $scope.status = status;
                $scope.data = data;
                $scope.result = data; // Result
            })
            .error(function(data, status) {
                $scope.data = data || "Request failed";
                $scope.status = status;         
            });
        }
    }]);

The HTML looks like this for the search form:

<div ng-controller="SearchCtrl">
    <form class="well form-search">
        <label>Search:</label>
        <input type="text" ng-model="keywords" class="input-medium search-query" placeholder="Keywords...">
        <button type="submit" class="btn" ng-click="search()">Search</button>
    </form>
    <div ng-model="result">
    {{result}}
    </div>
</div>

The error that undefined is not a function for this line: $http.post($scope.url, { "data" : $scope.keywords}).

Can anyone point me in the right direction?

It's also not doing the query string on the URL so it's not becoming part of the history stack.

The service looks like:

phonecatServices.factory('Phone', ['$resource',
  function($resource){
    return $resource('phones/:phoneId.json', {}, {
      query: {method:'GET', params:{phoneId:'phones'}, isArray:true}
    });
  }]);

So in short... why am I getting the undefined error? And why isn't the query string being passed?

1
  • on what line you are getting the error, better you add a demo. Commented Aug 18, 2014 at 9:30

3 Answers 3

2

mistake is here in this line -

phonecatControllers.controller('SearchCtrl', ['$scope', 'Phone', 

It should be

phonecatControllers.controller('SearchCtrl', ['$scope', '$http', 

you should pass $http, not phone

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

Comments

1

You need to initialize keywords before using it either in the controller:

phonecatControllers.controller('SearchCtrl', ['$scope', 'Phone', 
   function ($scope, $http) {
    $scope.url = 'phones.json';
    $scope.keywords = '';
    // The function that will be executed on button click (ng-click="search()")
    $scope.search = function() {

        // Create the http post request
        // the data holds the keywords
        // The request is a JSON request.
        $http.post($scope.url, { "data" : $scope.keywords})
        .success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
            $scope.result = data; // Result
        })
        .error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;         
        });
    }
}]);

Or in the HTML using ng-init

<div ng-controller="SearchCtrl">
    <form class="well form-search" ng-init="keywords:''">
        <label>Search:</label>
        <input type="text" ng-model="keywords" class="input-medium search-query" placeholder="Keywords...">
        <button type="submit" class="btn" ng-click="search()">Search</button>
    </form>
    <div ng-model="result">
    {{result}}
    </div>
</div>

As per the new Question here is the update:

You need to remove the double quotes from data and then pass the data something like this:

 $http.get($scope.url, { data : $scope.keywords})
        .success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
            $scope.result = data; // Result
 })

passing by RESTful approach

 $http.get($scope.url+'/'+$scope.keywords)
        .success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
            $scope.result = data; // Result
 })

6 Comments

Any ideas for the query string part? I've updated my question to show how am I trying to do it.
Doesn't add anything to the URL when I submit the form (but it does the search).
it wont add anything to the url it is sending it the request body
So how do I add it then? It should update the History so that if the user visits a result and then goes back it will return them to their search query.
Doesn't pass it :( and the cached JS has been cleared.
|
0

To pass the query string I've done:

phonecatControllers.controller('SearchCtrl', ['$scope', '$http', '$location', 
    function ($scope, $http, $location) {
        $scope.keywords = $location.search()['q'];
        $scope.search = function() {
            $location.path('/phones').search('q', $scope.keywords);     
            });

        }
    }]);

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.