将参数传递给来自控制器的angularjs中的资源工厂不工作

时间:2014-09-14 06:08:29

标签: html5 angularjs web-services rest twitter-bootstrap-3

将参数传递给angularjs中的$ resource。

点击后,我在UI中有一个搜索按钮,我想将输入参数传递给$ resource,后者将调用后端的REST服务。

当我在.query中传递一个字符串值时,它完全正常,但@issueinput没有被解析为输入值。

这是app.js

var myApp = angular.module('myApp',['ui.router', 'ngResource']);

myApp.factory('SearchService',function($resource){
    return $resource("http://localhost:8081/support/api/search/:issuename");
});

这是控制器

angular
  .module('myApp')
  .controller('searchCtrl',['$scope', '$window','SearchService', function($scope, $window, SearchService){

     $scope.data={};

    $scope.search = function(issueinput){    
     SearchService.query({issuename:'@issueinput'},function(response){
            $scope.data.issues = response; 
         });
    };
  }  
  ]);

这是UI html

<html>
<div id="searchpage">
     <label>Search</label> :<input data-ng-model="issueinput"></input> {{issueinput}}  
     <br/>
     <br/>
     <br/>
     <button data-ng-click="search(issueinput)">Search</button>
     <br/>
     <br/>
<ul>
  <li data-ng-repeat="s in data.issues">{{s.issueName}} : {{s.issueDescription}}</li>
</ul>


</div>
</html>

1 个答案:

答案 0 :(得分:3)

我通过进行以下更改来实现它

控制器

$scope.search = function(){  
SearchService.get({issuename: $scope.issueinput},function(response){
$scope.data.issues = response; 
});
app.js中的

$资源

myApp.factory('SearchService',['$resource',function($resource){
    return $resource("http://localhost:8081/support/api/issue/:issuename",{},
       {
        get:{
             method: 'GET',
             isArray:true}}
    );
}]);
相关问题