marcoslin/angularAMD

ControllerAs Syntax

Closed this issue · 7 comments

How Can I use "ControllerAs" Syntax with angularAMD.route?

I looked the documentation, but I didn't find it.

Tks.

I never tried this but it is probably not supported. I am guess that controllerAs is publishing the reference of the controller at the time of route creation which does not exists at that time.

What is your use case?

I guess so, I think that the Controller should be declared before for the ControllerAs works.
But, I'll do some tests and after I'll put the results here.
Another thing that I dont know if works... If I declare two Controller in one file, the AngularAMD will woks? How it differentiates?

Declaring two controller is find as far as angularAMD is concerned. You just need to watch out for the loading sequence, making sure that it is loaded before referencing it in AngularJS.

Personally, I moved away from declaring controller and return a function to use as controller per:
https://github.com/marcoslin/angularAMD#route-without-controller

Ok, two controllers is not the angularAMD concern, and I understand that.
About "Route Without Controller", I prefer to declare the controller's syntax.

I have a question about angularAMD.route, the field controller, would be a function? I guess that would be the place for declare the Controller's name, and I could be use for declare
"Controller As" syntax, like:

.state('pinkman', angularAMD.route({
    url: '/pinkman',
    controllerUrl:'controllers/pinkmanController',
    controller:'PinkmanController as pinkman',
    templateUrl:'views/pinkman/pinkman.html'
}));

Because, when I put just a name, like "PinkmanController", I receive an error, "PinkmanController is not a function".

So, I finish my tests, and really ControllerAs doesn't work putting

...
    controller:'PinkmanController as pinkman'
...

But, If I put the tag

<div  ng-controller="PinkmanController as pinkman">

in my HTML file, it works fine.

ControllerAs can be a future feature for AngularAMD.

Congrats for your lib, its a great job.

Tks.

ControllerAs syntax works! Example:

App.js

$stateProvider
        .state('walter', angularAMD.route({
            url: '/walter',
            controller:'WalterController',
            controllerAs:'walt',
            templateUrl:'views/walter/walter.html'
        }));

WalterController.js

define(['app'], function (app) {
    'use strict';
    app.controller('WalterController', function () {
        ...
    });
});

Great find, thank you. Should be added to README.