4

I want to set controller which name described in scope of another controller

JS file:

.controller('PageCtrl', [
  '$http',
  '$scope',
  '$routeParams',
  '$location',
  function($http, $scope, $routeParams, $location){
    $scope.CurrentPageCtrl = 'CompaniesCtrl';    
  }])

.controller('CompaniesCtrl', [
  '$http',
  '$scope',
  '$routeParams',
  '$location',
  function($http, $scope, $routeParams, $location){
    console.log('loaded');
  }
])

HTML file:

<ng-controller ng-controller = "PageCtrl">
    <ng-controller ng-controller = "{{CurrentPageCtrl}}">111</ng-controller>
</ng-controller>

But I get:

Error: [ng:areq] Argument '{{CurrentPageCtrl}}' is not a function, got undefined
John V
  • 875
  • 6
  • 12
  • use custom directive see here http://stackoverflow.com/questions/24762229/dynamic-ng-controller-name – Durgpal Singh Jun 03 '16 at 08:36
  • Possible duplicate of [AngularJS set ng-controller with scope variables](http://stackoverflow.com/questions/26410262/angularjs-set-ng-controller-with-scope-variables) – Cyril Cherian Jun 03 '16 at 08:37

1 Answers1

2

The thing is the controller would not be called until it is given in ng-controller in your code. So there is no question of $scope variable to be accessible in your view ie, your html. So you either must use the ngRouter or ui.router for specifying the controller during execution or you must use the global directive as given in the answer of the link below. Dynamic NG-Controller Name

Community
  • 1
  • 1
Tirthraj Barot
  • 2,671
  • 2
  • 17
  • 33