0

I am pretty new to Angular UI-ROUTER, I wonder how can I define same URL but with different parameters combination like:

$stateProvider
    .state("no_param",{
        url: "/param",
        /* if no param, goes here*/
        template: "<div ng-repeat='p in params'>{{p}}</div>",
        controller: function($scope, $stateParams){
            $scope.params = $stateParams;
        }
    })
    .state("one_param",{
        url: "/param?param1",
        /* if one param1, goes here*/
        template: "<div ng-repeat='p in params'>{{p}}</div>",
        controller: function($scope, $stateParams){
            $scope.params = $stateParams;
        }
    })
    .state("another_one_param",{
        url: "/param?param2",
        /* if one param2, goes here*/
        template: "<div ng-repeat='p in params'>{{p}}</div>",
        controller: function($scope, $stateParams){
            $scope.params = $stateParams;
        }
    })
    .state("two_param",{
        url: "/param?param1&param2",
        /* if param1 & param2, goes here*/
        template: "<div ng-repeat='p in params'>{{p}}</div>",
        controller: function($scope, $stateParams){
            $scope.params = $stateParams;
        }
    })

I tried this, but it does not work. Nothing shown, only display a blank page.

Then I set a breakpoint inside the controller, it turns out that the controller never get run when I navigate to a URL like /param?param2=test

Could anyone help?

1 Answer 1

1

What you are missing is the views property, here is a working example:

JSFiddle

angular.module('myApp', ['ui.state'])
    .config(['$stateProvider', '$routeProvider',
        function($stateProvider, $routeProvider) {
            $stateProvider
                .state('test', {
                    url: '/test',
                    views: {
                        'main': {
                            template: '<a ng-href="#/test/hello">param1</a> - <a ng-href="#/test/hello/world">param2</a>',
                        }
                    }
                })
                .state('one_param', {
                    url: '/test/:param1',
                    views: {
                        'main': {
                            template: '<a ng-href="#/test">home</a> <div ng-repeat="p in params">{{p}}</div>',
                            controller: function($scope, $stateParams) {
                                $scope.params = $stateParams;
                            }
                        }
                    }
                })
                .state('two_param', {
                    url: '/test/:param1/:param2',
                    views: {
                        'main': {
                            template: '<a ng-href="#/test">home</a> <div ng-repeat="p in params">{{p}}</div>',
                            controller: function($scope, $stateParams) {
                                $scope.params = $stateParams;
                            }
                        }
                    }
                });
        }
    ]);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, could you help pointing out(maybe rephrase here) which part of that link("as stated here:")should I pay attention to?
Exactly the Url Parameters section, look at the examples they are pretty self-explained
Sorry I did not quite catch that part, I try to figure out which part says that "You have to use another notation/syntax" and Why my way does not work
mmm no there should be another problem with your code, indeed you can use the ?param1 notation too. I should try something with a JSFiddle.
I wonder how can I tell it is param1 or param2 in url if I use your solution?
|

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.