0

I have a form, where in i submit a text using ng-click, now it calls a rest service and insert into the DB - working fine. at the same time, in same function, it calls another rest service but nothing is happening , i want to know how can I pass parameter to service???

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'}
                    })};
            }]);

in allresults, id is not coming or I dont know if controll is reaching here or not. post submission in "postmain" and query is working fine. please let me know how to do it?

I need to send "tag" which is "#scope.UserPost.postText" as param in controller

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

to service.js "allresults" and get the data back from rest service.

3
  • Do you mean you wan the URL to be formed as "/ngdemo/web/posts/result/:tag?id=someValue", if so what is the value of id Commented Mar 7, 2014 at 11:51
  • Sorry, its not id, its tag, instead of id, it shud be tag Commented Mar 7, 2014 at 13:27
  • url shud be /ngdemo/web/posts/result/:tag where tag is the string which i will be sending thru query param Commented Mar 7, 2014 at 13:29

2 Answers 2

2

Anything you pass to the service function that isnt explicitly specified is just always added as a query param, so you should just need to do something like:

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

Then call it like this

$scope.allresultsfinal = PostFactory.allresults.query(  
`{tag: $scope.UserPost.postText,  
`id:$scope.someId}//perhaps $routeParams.id ??  
`);
Sign up to request clarification or add additional context in comments.

1 Comment

No, it didnt work, allresults: $resource('/ngdemo/web/posts/result/:tag', {tag: '@tag'}, { query: {method: 'GET', params: {tag: '@tag'}, isArray: true }, create: {method: 'POST'} })}; and controller -- $scope.allresultsfinal = PostFactory.allresults.query({tag: $scope.UserPost.postText})
0

Try the below,

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

In controller

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

3 Comments

Didnt work! My controller code is, is there a problem???? -- @RequestMapping(value = "/result/{tag}", method = RequestMethod.GET, produces = "application/json") public @ResponseBody List<Place> results(@PathVariable("tag") String tag) { List<Place> places = null; System.out.println("tag : " + tag); try {
Shouldnt be the prob with controller, because it works independently, tag value is not reaching controller
I could see the following line in your code $scope.allposts.push($scope.UserPost); $scope.UserPost = ""; // You are making this object blank PostFactory.allresults.query({tag: $scope.UserPost.postText})// How do you expect to get the object here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.