【问题标题】:How to pass a query param from controller to service in angularjs?如何将查询参数从控制器传递到angularjs中的服务?
【发布时间】:2014-03-07 22:08:45
【问题描述】:

我有一个表单,在其中我使用 ng-click 提交文本,现在它调用休息服务并插入数据库 - 工作正常。同时,在同一个函数中,它调用了另一个休息服务,但什么也没发生,我想知道如何将参数传递给服务???

Controller.js

                app.controller('MyCtrl1', ['$scope', 'PostFactory', '$location', function ($scope, PostFactory, $location) {

                /* callback for ng-click 'createUser': */
                $scope.createPost = function () {
                    alert("in createPost" + $scope.UserPost.postText);
                    alert("in createPost" + $scope.UserPost);
                    PostFactory.postmain.create($scope.UserPost)
                    $scope.allposts.push($scope.UserPost);
                    $scope.UserPost = "";
                    $scope.allresultsfinal = PostFactory.allresults.query({tag: $scope.UserPost.postText})
                    $location.path('/view1');
                }

                $scope.allposts = PostFactory.postmain.query();


                /*UserFactory.get({}, function (userFactory) {
                 $scope.firstname = userFactory.firstName;
                 })*/
            }]);

Services.js

            'use strict';

            /* Services */

            var services = angular.module('ngdemo.services', ['ngResource']);
            //alert("In services");
            services.factory('PostFactory', ['$resource', function ($resource) {
                return  {

                    postmain: $resource('/ngdemo/web/posts', {}, {
                        query: {method: 'GET', isArray: true },
                        create: {method: 'POST'}
                    }),
                    allresults: $resource('/ngdemo/web/posts/result/:tag', {id: '@id'}, {
                        query: {method: 'GET', params: {id: '@id'}, isArray: true },
                        create: {method: 'POST'}
                    })};
            }]);

在所有结果中,id 没有出现,或者我不知道 controll 是否到达这里。在“postmain”中发布提交并且查询工作正常。请告诉我该怎么做?

我需要发送“标签”,即“#scope.UserPost.postText”作为控制器中的参数

$scope.allresultsfinal = PostFactory.allresults.query({tag: $scope.UserPost.postText})

到 service.js "allresults" 并从 rest 服务中取回数据。

【问题讨论】:

  • 你的意思是你想让 URL 形成为“/ngdemo/web/posts/result/:tag?id=someValue”,如果是,id 的值是什么
  • 对不起,它不是 id,它的标签,而不是 id,它应该是标签
  • url 应该是 /ngdemo/web/posts/result/:tag 其中标签是我将通过查询参数发送的字符串

标签: angularjs rest angularjs-directive angularjs-scope angularjs-ng-repeat


【解决方案1】:

您传递给服务函数但未明确指定的任何内容总是作为查询参数添加,因此您只需要执行以下操作:

allresults: $resource('/ngdemo/web/posts/result/:tag', {tag: '@tag'}, {  
`query: {method: 'GET',   
`params: {},   
`isArray: true },  
`create: {method: 'POST'}  
})};  

那就这样称呼吧

$scope.allresultsfinal = PostFactory.allresults.query(  
`{tag: $scope.UserPost.postText,  
`id:$scope.someId}//perhaps $routeParams.id ??  
`);

【讨论】:

  • 不,没用,所有结果:$resource('/ngdemo/web/posts/result/:tag', {tag: '@tag'}, { query: {method: 'GET ',参数:{tag:'@tag'},isArray:true},创建:{method:'POST'}})};和控制器 -- $scope.allresultsfinal = PostFactory.allresults.query({tag: $scope.UserPost.postText})
【解决方案2】:

试试下面,

allresults: $resource('/ngdemo/web/posts/result/:tag', {}, {
     query: {method: 'GET', isArray: true },
     create: {method: 'POST'}
})};

在控制器中

$scope.allresultsfinal = PostFactory.allresults.query({tag: $scope.UserPost.postText})

【讨论】:

  • 没用!我的控制器代码是,有问题吗??? -- @RequestMapping(value = "/result/{tag}", method = RequestMethod.GET,produces = "application/json") public @ResponseBody List results(@PathVariable("tag") String tag) {列表地点=空; System.out.println("标签:" + 标签);试试 {
  • 应该不是控制器的问题,因为它独立工作,标签值没有到达控制器
  • 我可以在您的代码中看到以下行 $scope.allposts.push($scope.UserPost); $scope.UserPost = ""; // 你将这个对象设为空白 PostFactory.allresults.query({tag: $scope.UserPost.postText})// 你希望如何在这里得到这个对象?
猜你喜欢
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多