【问题标题】:Reference error when searching on submit using AngularJS使用 AngularJS 搜索提交时的参考错误
【发布时间】: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,它会返回一个promis,所以你必须使用http.get().then(successscalback,errorcalback)

标签: angularjs angularjs-scope referenceerror


【解决方案1】:

尝试$scope.searchkeyword,您只使用searchkeyword

var url = 'http://localhost/json/search.php?params='+$scope.searchkeyword;
$http.get(url).function(response) { .....

【讨论】:

  • 错误:未定义响应。
  • 以同样的方式完成,但使用@MiTa 建议的回调,谢谢。
【解决方案2】:

更改以下代码

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

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

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    相关资源
    最近更新 更多