1

If I want to assign dynamic controller, I can do the following:

<div ng-controller="MainController">
  <div ng-controller="dynamicController"></div>
</div>

function MainController($scope){
  $scope.dynamicController = MyCtrl;
  $scope.instanceName = "ctrl"; // we'll use this later
}

function MyCtrl(){}

What can I do to make this work with the new "controller as" syntax?

This works fine: <div ng-controller="dynamicController as ctrl"></div>

But how to make ctrl dynamic too? Let's say I want it to have a name that $scope.instanceName holds.

Fiddle: http://jsfiddle.net/ftza67or/2/

There is an idea to make a custom directive that will create and compile html string, but it's an ugly way, let's pretend it does not exist.

3
  • Quick search on Google gave me toddmotto.com/digging-into-angulars-controller-as-syntax, and thinkster.io/egghead/experimental-controller-as-syntax Commented Dec 27, 2014 at 21:49
  • @elclanrs i know how to use "controller as" syntax. My question is how to use it with dynamic controller reference. Commented Dec 27, 2014 at 21:52
  • Ah, I misread. Maybe you can check the $controller service. Using the name of the function seems like trouble, I'd try to use the name of the registered controller. Commented Dec 27, 2014 at 22:14

2 Answers 2

1

This should work pretty much the same, but just remember that when you use controller as you can bind properties to this inside the controller to have them accessed by the scope/view.

function MainController ($scope) {
  $scope.dynamicController = MyCtrl;
  }

function MyCtrl($scope) {
  this.foo = "baz";
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="MainController">
  <div ng-controller="dynamicController as ctrl">
    {{ctrl.foo}}
  </div>
</div>

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

10 Comments

I just noticed that jsfiddle that i was using for testing was forked from one with old angular version. This works, yes.
BUT ) How to make controller instance name dynamic too? (the "as ctrl" part)
@Anri, why do you need the alias to be dynamic? It makes little sense. What are you trying to achieve?
@NewDev I have many "cards" in one page, and each card has it's own controller and cards are created in ng-repeater. So in card templates i want to reference correct controller. I guess I could use the same name for all, they are not nested YET, but still curious how to achieve that.
@Anri The alias with controller as is like a variable -- you can't have dynamic variable names; that wouldn't make sense
|
0

So I've looked into angular sources and found this. https://github.com/angular/angular.js/blob/bf6a79c3484f474c300b5442ae73483030ef5782/src/ng/controller.js

 if (isString(expression)) {
    match = expression.match(CNTRL_REG),
    constructor = match[1],
    identifier = identifier || match[3];
    expression = controllers.hasOwnProperty(constructor)
        ? controllers[constructor]
        : getter(locals.$scope, constructor, true) ||
            (globals ? getter($window, constructor, true) : undefined);
  .... and so on

Basically, ng-controller directive currently accepts strings or expressions, if it is an expression, it has to be a controller function reference. If it is a string, well, it will take identifier name as it was passed and I don't see any way to make it evaluate a variable with dynamic expression name.

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.