【问题标题】:passing parameters to resource factory in angularjs from the controller not working从控制器将参数传递给angularjs中的资源工厂不起作用
【发布时间】:2014-11-07 22:45:20
【问题描述】:

在 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>

【问题讨论】:

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


    【解决方案1】:

    我通过进行以下更改使其工作

    控制器

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

    app.js 中的$resource

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

    【讨论】:

    • 帮了大忙,就是把我的头发拉出来,然后找到了这篇文章。对我来说是在此行上添加参数 SearchService.get({issuename: $scope.issueinput},function(response){
    猜你喜欢
    • 2014-03-21
    • 2015-08-06
    • 2014-07-29
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 2015-09-13
    相关资源
    最近更新 更多