0

For example, in this official tutorial, only ONE controller PhoneListCtrl is assigned to the view /phones. What if I have multiple controllers for this view?

phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).

Thanks in advance!

1
  • Why not assign the controllers directly to the view? Commented Apr 27, 2014 at 16:40

1 Answer 1

1

There are plenty of ways to attack this concern.

  • You can use the ng-controller directive within a view:

    <div ng-controller="someOtherController">
        <input ng-model="propertyOfSomeOtherControllersScope" />
    </div>
    
    • You can use the angular-ui team's ui-router module. In short, this allows you to have multiple named views within a single "state", which are idiomatically like their "routes". Way too much going on with it to describe it in full here, but it's a great module for almost any project.

    • This is kind of the same as the first solution, but you could use ng-include to include another partial which explicitly declares its controller. Basically, in your main file: <div ng-include="'file2.html'"></div> and in file2.html: <div ng-controller="someOtherController"></div>.

These three things just jump right to mind, there are certainly other ways to go about this. And in some cases, you might want a directive instead of just another controller within a single view. Hopefully this gets you started in the right direction.

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

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.