点击角度工厂刷新资源查询?

时间:2016-02-24 23:14:30

标签: angularjs ngresource

我开始学习angularJS,我很难搞清楚如何将变量传递给工厂并在点击事件中刷新资源。

具体来说,我不知道如何将搜索变量从表单字段传递给工厂,而不是硬编码的' ???'到参数:{查询:' ???'} ...然后,在提交时,应该使用该搜索参数再次获取资源...我尝试使用" searchterms"变量在那里,但它输出" searchterm"在控制台中未定义。

    <html ng-app="notmuchApp">
  <head>
    <meta charset="utf-8">
    <title>notmuch sinatra</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <script src="js/angular.js"></script>
  <script src="js/angular-resource.js"></script>
  <script src="js/ui-bootstrap-tpls-1.1.2.js"></script>
    <script>

  var notmuchApp = angular.module('notmuchApp', ['ui.bootstrap','ngResource']);
  notmuchApp.controller('handleSearch', ['$scope', 'getSearch',
    function ($scope, getSearch) {
        $scope.searchresults = getSearch.query();
    }]);

  notmuchApp.factory('getSearch', function ($resource) {
    return $resource('/notmuch/search', {}, {
        query: { method: 'GET', params: {query:'???'}, isArray: true },
    })
});

  </script>
  </head>
 <body >
  <div ng-controller="handleSearch">
  <form ng-submit="getSearch">
    <input type="text" ng-model="searchterm"> 
    <input type="submit" value="Search">
  </form>
    Results:
    <ul>
      <li ng-repeat="searchresult in searchresults">
         {{searchresult.date_relative}} {{searchresult.authors}} {{searchresult.subject}}
      </li>
    </ul>
  </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

通常,您的工厂不会直接返回结果,而是&#34;服务&#34;具有&#34; getSearch&#34;的对象方法:

public function rules()
{
    return [
        'images.*' => 'mimes:jpg,jpeg,png'
    ];
}

另外,你处于异步执行的世界,所以我冒昧地改变你的代码来自&#34; $ scope.results = getResults()&#34;正确处理承诺(加上错误处理)。