角度 - 从工厂检索json

时间:2014-06-20 01:36:49

标签: angularjs angularjs-scope

尝试让工厂(下方)检索json文件。你知道什么是错的吗? (Angular v.1.3.x-beta)

HTML

<div class="search">
  <input type="text" ng-model="query" ng-model-options="{debounce:750}" ng-change="updateSearch()"/>
</div>
{{results | json}}

JS

directory.controller('DirectorySearchController', function($scope, searchFactory) {
  $scope.updateSearch = function() {
    $formatted = $scope.query.replace(/\s{1,}/g, '+');
    if($scope.query != '') {
        $scope.results = searchFactory.simpleSearch($formatted);
    } else {
        $scope.results = '';
    }
  };
});

directory.factory('searchFactory', function($http) {
  var urlBase = 'http://example.com/api';
  var searchFactory = {};
  searchFactory.simpleSearch = function (keyword) {
    return $http.get(urlBase + '/search/' + keyword);
  };
  return searchFactory;
});

1 个答案:

答案 0 :(得分:0)

查看您的代码,工厂返回一个承诺,并且在新版本的角度中无法使用自动承诺展开。试试这个

searchFactory.simpleSearch($formatted).success(function(data){
  $scope.results=data;
});