1

I'm setting up an angularjs app that has multiple vertical columns that display on the same page and based on what item is clicked a new or different column will expand.

Here's my jsbin example.

How do you recommend setting this up in angular? Should this be setup using ui-router with nested children or do you have a better suggestion? Examples are appreciated.

Thanks :)

2 Answers 2

4

I think this is a great example of a ui-router nested state-machine.

I have knocked up an example of kind of the ui-router state configuration you might want and examples of the corresponding controllers that would expose the data to the appropriate scopes.

Please note that this is definitely not the finished code structure, but it should give you a good idea of the syntax and how to generally implement nested ui-router states with resolved data etc.

The code is all coffeescript. If you need it in javascript, here is a transpiler for you. The template snippet example is Jade, here is a jade to html converter

UI-Router Config

angular
.module 'app', ['ui.router']
.config ($stateProvider) ->
    $stateProvider

        .state 'root',
            url:'/'
            templateUrl: "root.html"
            controller: 'rootCtrl'
            resolve:
                items:(dataService) -> dataService.getPrimary

        .state 'root.primary',
            url:'/{primary}'
            templateUrl: "primary.html"
            controller: 'primaryCtrl'
            resolve:
                items:(dataService, $stateParams) -> 
                    dataService.getSecondary $stateParams.primary

        .state 'root.primary.secondary',
            url:'/{secondary}'
            templateUrl: "secondary.html"
            controller: 'secondaryCtrl'
            resolve:
                items:(dataService, $stateParams) -> 
                    dataService.getTertiary $stateParams.secondary

        .state 'root.primary.secondary.tertiary',
            url:'/{tertiary}'
            templateUrl: "tertiary.html"
            controller: 'tertiaryCtrl'
            resolve:
                content:(dataService, $stateParams) -> 
                    dataService.getContent $stateParams.tertiary

Controllers

angular
.module 'app'
.controller 'rootCtrl', ($scope, items) ->
    $scope.primaryItems = items.data

angular
.module 'app'
.controller 'primaryCtrl', ($scope, items) ->
    $scope.secondaryItems = items.data

angular
.module 'app'
.controller 'secondaryCtrl', ($scope, items) ->
    $scope.tertiaryItems = items.data

angular
.module 'app'
.controller 'tertiaryCtrl', ($scope, content) ->
    $scope.content = content.data

root.html

ul
    li(ng-repeat="item in primaryItems track by $index")
        a(ui-sref="root.primary({ primary: '{{item}}'})")

primary.html

ul
    li(ng-repeat="item in secondaryItems track by $index")
        a(ui-sref="root.primary.secondary({ secondary: '{{item}}'})")
Sign up to request clarification or add additional context in comments.

6 Comments

Interesting. How could the states be loaded into the root.html template and swapped out of the other templates based on a different parent being clicked.
I have updated the code to make it more generic. The initial entry url will determine the initial state although regardless you will see the primary items because the root templates url='/' loads them so they will always be available by default. The ui-sref attrib transitions to the appropriate state when the user click on a link (passing in the value of the clicked item as a state param). The html templates will need to nested via <div ui-view />
Added the code to plnkr, but couldn't get the resolve working.
Have you written the dataService yet? You will need to write this service to supply the actual data values you need. The code above is for illustration only, it is not intended to actually work as it stands but I hope it will give you an idea of how to approach the problem. Good luck.
@Jeffrey Did my answer help you? I would appreciate you accepting the answer or perhaps an upvote just for the effort :-) Thanks.
|
0

Ui-Router makes sense to me...one of the advantages being that it sort of wraps up specifying the controller and everything nicely so you can add the interactive elements to the next column.

If that type of thing isn't important and you just want content, tabs could easily be used.

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.