简体   繁体   English

用户输入未从$ scope。$ watch绑定

[英]User input not binding from $scope.$watch

Just getting started with Angular and I've spent the last 2 days trying to figure out how to bind data from a new search through a service. 刚开始使用Angular,我花了最后两天的时间来弄清楚如何通过服务绑定来自新搜索的数据。 I had the search working before with the following code before using a service: 在使用服务之前,我曾使用以下代码进行过搜索:

SearchController.js SearchController.js

 function SearchController($scope, $http){ $scope.search = "" $scope.getGames = function (){ return $http.get("https://igdbcom-internet-game-database-v1.p.mashape.com/games/?fields=name%2Crating%2Ccover%2Curl%2Csummary%2Cfirst_release_date&limit=50&offset=0&order=release_dates.date%3Aasc&search=" + $scope.search, {"headers": { "x-mashape-key": "KEY", "accept": "application/json", } }) .success(function(resp){ $scope.games = resp }) .error(function(data){ console.log(data) }) } $scope.getGames() }; SearchController.$inject = ['$scope', '$http'] angular .module('app') .controller('SearchController',SearchController) 
 search.html <div class="container"> <div ng-controller="SearchController"> <div class="col-md-6 col-md-offset-4"> <h1>Search for Game</h1> <form name="form"> <input name="search" ng-model="search" ng-change="getGames()" ng-model-options="{debounce: 1000}" placeholder="Type Game" minlength="3" required="required" /> <div ng-messages="form.search.$error" ng-if="form.search.$touched"> <div ng-message="required">Please type a game to search.</div> <div ng-message="minlength">3 characters required</div> </div> </form> </div> <div class="row fix-heights"> <div class="col-md-6" ng-repeat="game in games | filter: search" class="row-eq-height"> <br> <div class="media"> <div class="media-left"> <img class="pull-left" src="https://res.cloudinary.com/igdb/image/upload/t_thumb/{{ game.cover.cloudinary_id }}.jpg"> </div> <div class="media-body"> <p>Title: <a href="{{game.url}}">{{ game.name }}</a></p> <p>Release Date: {{ game.first_release_date | date:'mediumDate'}} <p>Short Description: {{ game.summary }}</p> </div> </div> </div> </div> </div> </div> 

So my first attempt was successful but when I tried to move the code to a service I am unable to automatically update and bind the data from the new search. 因此,我的第一次尝试是成功的,但是当我尝试将代码移至服务时,我无法自动更新并绑定新搜索中的数据。 I've tried to use $scope.$watch and I can see the url change in the console but the results do not populate in my search.html. 我尝试使用$ scope。$ watch,但可以在控制台中看到url的更改,但结果未填充在search.html中。 Below are the new changes. 以下是新的更改。

 function SearchController($scope, $http, GetGameService){ $scope.search = "" search = $scope.search GetGameService.getGames(search) .success(function(resp){ $scope.games = resp console.log(resp) }) .error(function(data){ console.log(data) }) $scope.$watch('search', function(){ search = $scope.search GetGameService.getGames(search) }) }; SearchController.$inject = ['$scope', '$http', 'GetGameService'] angular .module('app') .controller('SearchController',SearchController) /////////GetGameService.js function GetGameService($http){ this.getGames = function(search) { return $http.get("https://igdbcom-internet-game-database-v1.p.mashape.com/games/?fields=name%2Crating%2Ccover%2Curl%2Csummary%2Cfirst_release_date&limit=50&offset=0&order=release_dates.date%3Aasc&search=" + search, {"headers": { "x-mashape-key": "KEY", "accept": "application/json", } }) } } GetGameService.$inject = ["$http"] angular .module('app') .service("GetGameService", GetGameService); 
 <div class="container"> <div ng-controller="SearchController"> <div class="col-md-6 col-md-offset-4"> <h1>Search for Game</h1> <form name="form"> <input name="search" ng-model="search" ng-model-options="{debounce: 1000}" placeholder="Type Game" minlength="3" required="required" /> <div ng-messages="form.search.$error" ng-if="form.search.$touched"> <div ng-message="required">Please type a game to search.</div> <div ng-message="minlength">3 characters required</div> </div> </form> </div> <div class="row fix-heights"> <div class="col-md-6" ng-repeat="game in games | filter: search" class="row-eq-height"> <br> <div class="media"> <div class="media-left"> <img class="pull-left" src="https://res.cloudinary.com/igdb/image/upload/t_thumb/{{ game.cover.cloudinary_id }}.jpg"> </div> <div class="media-body"> <p>Title: <a href="{{game.url}}">{{ game.name }}</a></p> <p>Release Date: {{ game.first_release_date | date:'mediumDate'}} <p>Short Description: {{ game.summary }}</p> </div> </div> </div> </div> </div> </div> 

Apologies for any wrong format and many thanks for any help! 抱歉任何格式错误,非常感谢您的帮助!

The primary error is you are missing the $scope.games assign inside your $watch 主要错误是你缺少了$scope.games您的内部分配$watch

I'm not sure whether you really want to call getGames on init, or intend to use it as a function. 我不确定您是真的要在init上调用getGames ,还是打算将其用作函数。

The controller can be reorganized to reduce code replication 可以重组控制器以减少代码复制

function SearchController($scope, $http, GetGameService){

  $scope.search = ""

  // getGames(); // if you need to call on init, call here

  $scope.$watch('search', function(){
    getGames();
  })

  function getGames() {
    return GetGameService.getGames($scope.search)
      .then(function(resp){ // it's better to use .then than .success
        $scope.games = resp
        console.log(resp)

      }, function(data){
        console.log(data)
      })
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM