0

I need to have a url structure in place for hierarchical categories using angularjs ui-router.

Example;

Category tree is like below:

  • Root
  • -Application 1
  • --Category level 1
  • ---Category level 2
  • ----Category level 3
  • -Application 2
  • -Application 3

The url xyz.com/application-1/category-1 will display articles in category level 1, similarly xyz.com/application-1/category-1/category-2/category-3 will display articles in category level 3. This can go upto the 4 or 5 levels of categories.

According to my understanding, i will have to define states for each of the category levels and all those states can access the same template and controller. Is there a better, smarter way of implementing these states?

2 Answers 2

1

What about use angular ui-router and abstract routing?...something like:

app.config([
    "$stateProvider",
    "$urlRouterProvider",
    "$ocLazyLoadProvider",
    "$locationProvider",
    function (
        $stateProvider,
        $urlRouterProvider,
        $ocLazyLoadProvider,
        $locationProvider) {

        $urlRouterProvider.otherwise("/index/main");


        $stateProvider

            .state("application1", {

                abstract: true, // <-- MEAN IT?S  A PARENT URL / TEMPLATE
                url: "/index",
                templateUrl: "/app/view/template/common/application1.html"
            })
            .state("application1.category1", { //<-- MEANS IT'S A CHILD OF index FATHER
                url: "/main",
                templateUrl: "/app/view/template/application1/category1.html",

                controller: "HomeController",
                controllerAs: "homeCtrl"


            })

So the index routing marked as abastract is the 'father' of multiple child

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

1 Comment

Yes.. I have this in place already. Please check Charan Cherry's answer below and my comment on it. That is more closer to my problem. Thank you!
0

Use stateProvider with nested states and views https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views

$stateProvider.state('/home', {
     url: '/home',
     templateUrl: '',
     controller: ''
})
.state('home.app1', {
     url: '/home/app1',
     templateUrl: '',
     controller: ''
})
.state('home.app1.cat1', {
     url: '/home/app1/cat1',
     templateUrl: '',
     controller: ''
})
.state('home.app1.cat1.cat2', {
     url: '/home/app1/cat1/cat2',
     templateUrl: '',
     controller: ''
})
.state('home.app1.cat1.cat2.cat3', {
     url: '/home/app1/cat1/cat2/cat3',
     templateUrl: '',
     controller: ''
})
.state('home.app2', {
     url: '/home/app2',
     templateUrl: '',
     controller: ''
})
.state('home.app3', {
     url: '/home/app3',
     templateUrl: '',
     controller: ''
})
$routerUrlProvider.otherwise('/home'); // if no path matched

I didn't mention named views. you can go through the link and It helps a lot.

But the above makes heavy structuring of states. So, you need to display a relevant info in a single state. For example, if there are three categories, and you want to show in single page then you can define a state like this.

.state('home.app1',{
   url: '/home/app1',
   views: {
    "view1": {
      templateUrl: 'cat1.html',
      controller: ''
    },
    "view2": {
      templateUrl: 'cat2.html',
      controller: ''
    },
    "view3": {
      templateUrl: 'cat3.gtml',
      controller: ''
    }
   }
})

Or you can have a single parent controller for all the views.

now, in html you will have

<div>
  <div ui-view="view1"></div>
  <div ui-view="view2"></div>
  <div ui-view="view3"></div>
</div>

3 Comments

Yes this is how i had thought of doing it. But the problem here is, if we add a new level of category, lets say category 5.. then i would need to add a new state. Was thinking if we can avoid this somehow..
Yes, you can have views in a single state and can display content. But if you need to show the added category 5 in separate state, you must add another state. Actually, in app1 state, you can have 3 views and display each category in separate views
Ok that means i am at a bottleneck and will have to add a state every time a new level of category is added and use the same view and controller as my display will be the same for all levels of categories.

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.