1

I am creating an AngularJS app with a restful-back end. I have 4 types of users for which each component/page may have significantly differnt behavior. What is the best approach to implement this?

Do I have a single html/partial and assign a different controller per role with different implementations per method? Should I have a completely separate HTML for each role and a different controller? Should I just use ifIsInrolecommands?

1 Answer 1

2

I've tried both independent controllers, and a single controller with conditionals. It's not fun dealing with slightly different behavior with types and modes. Different controllers will have a lot of repeated code. A single controller with ifs for each type will get very wordy.

My suggestion is to write out a couple of different controllers for two of your user types. When you see the equivalent behavior, move it into a parent controller and make the other two into children. Then finish your remaining two controllers as children of the parent controller. You should see the payoff pretty quickly.

As for the HTML, it's likely best to have different pages/partials for each. ng-include and ng-if would be nice for this.

Here's a quick explanation of parent/child controllers.

And the template design pattern will be pretty helpful. I expect it to work properly between parent and child controllers.

I'm more than happy to hear other opinions since I've hit this issue a few times, myself.

1
  • 1
    I agree with this, and if you use ui-router, there's good support for this approach.You can use an abstract parent state with a controller to share a controller between two views, or to share common functionality, or you can reuse controllers on separate views and configure them via a state's data object.
    – vrostu
    Commented Jun 1, 2015 at 21:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.