1

Here is my Angularjs Controller Code:

app.controller('listAdsController', ['$scope', 'myService', '$routeParams', '$location', function ($scope, myService, $routeParams, $location) {

myService.getAdsByCategory({categoryId : $routeParams.categoryId});
$scope.totalItems = 64;

$scope.pageChanged = function () {        
    var path = $location.path();
    $location.path(path).search('page', $scope.currentPage);
};

$scope.maxSize = 10;
$scope.bigTotalItems = 175;
$scope.currentPage= 1;
$scope.itemsPerPage = 50;
}]);

Here in pageChangedfunction, I am trying to change my Url according to page number by using querystring like below:

//localhost/category/property/4/house-for-rent?page=2

Here is my routing config for that Url:

$routeProvider.when("/category/:category/:categoryId/:urlName", {
        controller: "listAdsController",
        templateUrl: "app/views/category-ads.html"
    });

Now below is my Web Api 2 Controller:

    [HttpGet]
    public IHttpActionResult GetAllByCategory([FromUri] int categoryId, int? page = null)
    {
       //Code
    }

The problem is, page parameter is always always null no matter what I am passing in it. Can you someone guide me what I am missing here?

3
  • try Localhost/GetAllByCategory/1/2, if that works I have a pretty good explanation as to why Commented Sep 22, 2015 at 20:37
  • Your question is missing some essential parts: how does your Angular code invoke the Web API and how are the Web API routes configured? The Angular code you posted does not call the API. It only routes to a view with template app/views/category-ads.html and assigns the listAdsController to it (an Angular controller that is, not the Web API controller). Commented Sep 22, 2015 at 23:17
  • @venerik I am getting WebApi by using my myService Commented Sep 23, 2015 at 5:53

1 Answer 1

0

The problem was very small. I was just passing the querystring to url but was not getting it from the routeParam.

The solution is:

$scope.categoryAds = jetService.getAllByCategory({ categoryId: $routeParams.categoryId, page: $routeParams.page });

You need to get page from the querystring pass it to your service.

Sign up to request clarification or add additional context in comments.

Comments

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.