2

Hi I have a form in which I am sending a searched term and category id . It works fine as I am sending both things but if a user does not enter any thing only 'category id' goes, and in back end if searched term is empty all results are display to user.

But if I send an empty string in route params I redirect to myindex page. This is my app.js code.

  when('/subcategory/:subcatId/:search', {
                templateUrl: 'partials/subcategory.html',
                controller: 'SubCategoriesController'
            }).

My form

   <form name="searchCategoryForm">
                <div class="col-md-9 col-xs-10 col-sm-10">
                    <input class="form-control" placeholder="Find your item here...." ng-model="search" type="text" style="color:#09669c;">
                </div>
                <div class="col-md-2 col-xs-2 col-sm-2">
                    <a class="btn btn-primary" role="button" href='#/subcategory/{{cats[clicked_category].id}}/{{search}}'> Search</a>                  
                </div>
            </form>

My controller.js

$scope.search_term = $routeParams.search;
    $scope.category_id = $routeParams.subcatId;

    /* Search category form */


    $scope.search_category = function () {
        $http({
            method: 'GET',
            url: 'abc',
            params: {"name": $scope.search_term, "category_id": $scope.category_id},
            headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': $rootScope.keyword_auth_token}
        })
                .success(function (data) {
                    $scope.search_category_result = data.items;
                    console.log($scope.search_category_result);
                    })
                .error(function (data) {
                    console.log(data);
                });
    };
    /* Search category form ends here*/
    if ($scope.search_term && $scope.category_id)
    {
        $scope.search_category();
    }
Usman Iqbal
  • 2,379
  • 5
  • 26
  • 50

1 Answers1

1

I believe you should mark your search terms as optional by suffixing it with a question mark (?):

when('/subcategory/:subcatId/:search?', {
    templateUrl: 'partials/subcategory.html',
    controller: 'SubCategoriesController'
}).

See this answer for more info: Can angularjs routes have optional parameter values?

Community
  • 1
  • 1
Robba
  • 7,684
  • 12
  • 48
  • 76
  • Hats Off Sir :) – Usman Iqbal Dec 15 '16 at 07:56
  • Yes it's here in the docs: https://docs.angularjs.org/api/ngRoute/provider/$routeProvider under the Methods `when(path,route)` section for the `path` param. – Robba Dec 15 '16 at 07:58
  • can I ask one more question if I send the empty string the url becomes something like this subcategory/1/ I want to remove the last '/' – Usman Iqbal Dec 15 '16 at 10:48
  • Not sure if you can do that with the one route, but I'm pretty sure you can just add a second route without the `/:search` suffix that uses the same template and controller. – Robba Dec 15 '16 at 10:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130684/discussion-between-usman-iqbal-and-robba). – Usman Iqbal Dec 15 '16 at 10:57
  • Note that when you add a new route with the same template and controller, you should make the *search* parameter required again or there will be two routes that match the same url. – Robba Dec 15 '16 at 13:38