AngularJS没有将ng-model绑定到范围

时间:2014-10-22 13:07:30

标签: angularjs ionic-framework angular-ngmodel

我的搜索功能有以下代码

<ion-content class="has-header" id="content" push-search>
  <div id="search-bar">
   <div class="item item-input-inset">
     <label class="item-input-wrapper" id="search-input">
      <i class="icon ion-search placeholder-icon"></i>
      <input type="text" placeholder="Search" ng-model="query" ng-change="search()">
     </label>
    </div>
   </div>
  <div>
   <// code for displaying search results//>
</ion-content>

搜索控制器

.controller('SearchCtrl', function($scope, SearchFactory) {
  var doSearch = ionic.debounce(function(query) {
    console.log($scope);
    $scope.results = SearchFactory.get({'query':$scope.query});
  }, 500);
  $scope.search = function() {
    doSearch($scope.query);
  }
})

搜索工厂:

.factory('SearchFactory', function($resource) {
   return $resource(url.concat('/paths/search/:query'), 
                   {query: '@query' } ,
                   { get: { method: 'GET' , isArray: true} }
           );

})

当我打电话给搜索时,我的$ scope中没有$ scope.query:  (见)http://i.stack.imgur.com/MuxRt.png

2 个答案:

答案 0 :(得分:4)

它解决了。有关详情http://jimhoskins.com/2012/12/14/nested-scopes-in-angularjs.html

,请参阅此链接

答案 1 :(得分:0)

简短回答是“查询”需要是范围搜索对象中的字段,以便通过引用而不是值传递,例如:

.controller('SearchCtrl', function($scope, SearchFactory) {
  $scope.searchParams = {};
  $scope.searchParams.query = '';

  var doSearch = ionic.debounce(function(query) {
    console.log($scope);
    $scope.results =SearchFactory.get({'query':$scope.searchParams.query});
  }, 500);