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?