Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to define separate controller for an included view in Angular.js?
    primarykey
    data
    text
    <p>I have two separate views (named view1.html and view.html) with separate controllers (named controller1 and controller2) and they are routed using config:</p> <pre><code>myApp.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/view1', {templateUrl: 'view1.html', controller: controller1}). when('/view2', {templateUrl: 'view2.html', controller: controller2}). otherwise({redirectTo: '/view1'}); }]); </code></pre> <p>view1 and view2 have a shared part of template code, so I extracted it into a new template view (named view3.html) and use ng-include in view1.html and view2.html:</p> <pre><code>&lt;span ng-view=""&gt; ... &lt;span ng-include="'view3.html'"&gt;&lt;/span&gt; .... &lt;/span&gt; </code></pre> <p>also view3.html logic is independent from the controller1 and controller2, so the scope variables needed in veiw3.html is repeated in both of the controllers:</p> <pre><code>function controller1($scope){ ... some code to calculate $scope variables for view3 ... } function controller2($scope){ ... same code to calculate $scope variables for view3 ... } </code></pre> <p>My question: is there any way to extract repeated codes in controllers into a separate controller for view3? I add ng-controller for view3 but it doesn't work:</p> <pre><code>&lt;span ng-controller="controller3"&gt; view3.html template codes here &lt;/span&gt; </code></pre> <p>I guess it's because view3.html is included in an element with ng-view directive and can not have a separate controller.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload