【发布时间】:2014-09-08 05:55:56
【问题描述】:
我在使用 angularJS 提交搜索时遇到问题
这里是html,包含模块和控制器,
<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>
搜索功能:
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); });
}}]);
当我提交时,它给出了错误
ReferenceError: 未定义搜索关键字
感谢您的帮助。
【问题讨论】:
-
$scope.searchkeyword 应该在 http.get
-
谢谢,删除了那个错误,但没有进入响应/失败功能。
-
$http.get('localhost/json/search.php?params='+$scope.searchkeyword).....
-
你不应该将回调传递给http.get,它会返回一个promis,所以你必须使用http.get().then(successscalback,errorcalback)
标签: angularjs angularjs-scope referenceerror