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();
    }

1 Answer 1

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?

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

6 Comments

Hats Off Sir :)
Yes it's here in the docs: docs.angularjs.org/api/ngRoute/provider/$routeProvider under the Methods when(path,route) section for the path param.
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 '/'
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.
|

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.