1

I am pretty new to AngularJS. I am using ui-router and node.js as my UI server for API calls to another server. Right now, I am having trouble appending query strings to the URL depending on user input (dropdown selections).

My AngularJS controller code:

$scope.getResults = function() {
    server.getResults($scope.myColor, $scope.mySize)
    .success(function(data) {
        results = data;
    });
};

AngularJS service for the above function:

app.factory('server', ['$http', function($http){
    return { 
        getResults : function(color, size) {
            var req = {};
            req.color = color;
            req.size = size;

            return $http({
                method: 'GET', 
                url: 'results',
                params : req
            });
        }
    }
}]);

ui-router in Angular:

$stateProvider.state('home', {
    url: '/home',
    templateUrl: '/home.html',
    controller: 'MainCtrl',
    data:{ 
       pageTitle: 'blah' 
    }
})

In Node.js, I have my route like this:

app.get("/results", function (req, res) {

    var api = 'some api call/' + req.query.color + '/' + req.query.size;

    request(api, function (error, response, api) {

        if (!error && response.statusCode == 200) {
            res.json({
                Color: req.query.color, 
                Size: req.query.size,
                Answer: api
            });
        }
    });
});

I want to have the browser url go from /home to /home?color=Red&&size=Large

Obviously I am not doing this right, the browser url is not dynamic right now. Please show how to do this correctly.

1 Answer 1

1

Do use reloadOnSearch: false options to stop reloading current route when changes occurs in state parameters.

$stateProvider.state('home', {
    url: '/home?color&size',
    templateUrl: '/home.html',
    controller: 'MainCtrl',
    data:{ 
       pageTitle: 'blah' 
    },
    reloadOnSearch: false //would not refresh state, for location changes.
})

Controller

$scope.getResults = function() {
    //change parameter of URL usingn $location.search.
    $location.search('color', $scope.myColor);
    $location.search('size', $scope.mySize);
    server.getResults($scope.myColor, $scope.mySize)
    .success(function(data) {
        results = data;
    });
};
Sign up to request clarification or add additional context in comments.

9 Comments

@jebmarcus Glad to know that. Thanks :-)
@PankajParker btw the url changes as expected, but when i copy the url and paste it in a new tab, the server doesnt get called like it would normally
any way to fix that?
@jebmarcus on initial data call do take parameter from state parameters like $stateParams.color & $stateParams.size to read those parameter & pass in the ajax call..
@PankajParker Im a bit confused about what you mean by initial data call. I think the problem here is that the url right now is like this: "localhost:3000/#/home?color=Red&size=Large" but im making the return $http I have url: 'results' not 'home
|

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.