2

Is there any way that I can define two controller for the same templateUrl in angularjs using stateProvider like below?

//State Provider for dashBoard Screen
.state('dashBoard', {
    cache : false,
    url : "/dashBoard",
    templateUrl : dashBoardHtml,
    controller : ["FirstController","SecondController"]
})

The data binding logic is to heavy in my controller, the number of line is more than 1000, I have optimised, reduced cyclometic complexity everything. But now to make it more modularize I need to split the controller without change in the view like by not using any nested views.

Is there any way to define multiple contollers to same templateURL/html?

4
  • you want both controllers at once or seperate? Commented Mar 9, 2017 at 6:11
  • You could define one controller in the view and the other in the state Commented Mar 9, 2017 at 6:13
  • Try use multiple view in state Commented Mar 9, 2017 at 6:21
  • I think you need to use provider or service to modularize your app. Commented Mar 9, 2017 at 6:25

4 Answers 4

1

I think you can put them into several services for business logic or directives for UI logic to make your code more clear and tidy.

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

1 Comment

This is probably the best solution.
1

This will work for you but if you want then you can use ng-controller directive as well

$stateProvider.state('view', {
    url: "/url",
    views: {
        'menuContent': {
            templateUrl: "template",
            controller: ['ctrl1', 'ctrl2']
        }
    }
});

example using diretive

    <div ng-controller="testController2">
            <!-- your view content -->
     </div>

    <div ng-controller="testController1">
            <!-- your view content -->
   </div>

Comments

0

You can only assign one controller from the state and other one inside template like this

.state('dashBoard', {
    cache : false,
    url : "/dashBoard",
    templateUrl : dashBoardHtml,
    controller : FirstController
})

template

<div data-ng-controller="SecondController">
        <!-- your view content -->
    </div>

Comments

0

your can assign controller on html

<div ng-controller="controller1"></div>

and on same HTML on other div

 <div ng-controller="controller2"></div>

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.