ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

Ability to use Controller As syntax in routing

Open DServy opened this issue 13 years ago • 14 comments

With angular 1.2rc2

.state('index',{
  url:"/",
  templateUrl:"views/index.html",
  controller:"IndexCtrl",
  controllerAs:"index",
})

Doesn't seem to want to work, and declaring controllers on the includes of files leads to controller not found issues when using the back button for navigation. (Edit, this issue was caused by not registering a controller with the app using the app.controller('name', function) )

DServy avatar Oct 01 '13 21:10 DServy

+1

gendalf avatar Oct 06 '13 15:10 gendalf

I haven't really looked at Angular 1.2 yet, which I understand is where this is implemented. Is there any reference to how it should work or what it should do?

nateabele avatar Oct 06 '13 22:10 nateabele

It's been around in the 1.1.x code for a while:

https://groups.google.com/d/msg/angular/84selECbp1I/jcrfkhBSIkcJ

My 0.02 is that it is "not the angular way" because it helps you tie directives to contexts other than their own controllers in a way that you really shouldn't do.

(edit: pointed the link to the beginning of the discussion)

laurelnaiad avatar Oct 06 '13 22:10 laurelnaiad

Its been implemented since 1.1.5

https://github.com/angular/angular.js/commit/cd38cbf975b501d846e6149d1d993972a1af0053

and referenced in their Google I/O

http://www.youtube.com/watch?v=HCR7i5F5L8c

There is a work around, mainly by calling the controllers on the includes themselves, which isn't super bueno in my book but it gets the job done.

My 0.02, it prevents the scope from being a messy jumble pile of annoyance when trying to nest multiple chucks of independent code, and has more of a "widget" feel to implementation. Any naysayer on the "Controller As" syntax should give it a try.

You can also write controllers as classes and assign things to prototypes, which makes me feel pretty baller.

DServy avatar Oct 07 '13 15:10 DServy

@DServy,

You can also write controllers as classes and assign things to prototypes, which makes me feel pretty baller.

Interesting. Can you decribe or point to information on how "controller as" enables "classes" and prototypes to be used that is different from how they are available in regular-controller-land?

I'm proud of my "messy jumble piles of annoyance" but I do seek to keep business logic out of the ui so I try to keep the piles small and focused. If directive code is worried about which contoller to address then I feel I have a bad design.

But if controller as enables better code reuse then thats a different story.

laurelnaiad avatar Oct 07 '13 16:10 laurelnaiad

@stu-salsbury

http://www.youtube.com/watch?v=tTihyXaz4Bo

Pretty good, short, video on some of the benefits on using Controller As

I personally wrap apps with a base controller that assigns scope variables, and then wrap Controller As instances of "apps" or "widgets" inside the base controller's scope with ng-includes. That way I can use the scope as a kinda "global" setter because template bindings look through the entire scope tree to find values, and I can assign those values in one place and really only have to worry about one scope. I've had a lot of success with getting multiple different "widgets" to come together and work well without conflict (for instance, a controller that deals with a google map, and a controller that deals with rendering and displaying vacation packages).

Also, it helps make templates a bit more readable when working with a larger team. There is no doubt where the {{map.publications}} value is coming from, no matter how many "Widgets" I have on the page. However, if I just put {{publications}} on the page, that variable could be coming from my map controller, or my package controller, or even a signup controller, and if you are not familiar with how it was built or whats going on, you have to take time to figure that out.

Basically, its just angular namespacing, and who isn't a fan of namespacing?

DServy avatar Oct 07 '13 17:10 DServy

+1

randallmeeker avatar Oct 11 '13 20:10 randallmeeker

Nothing needs to change. I'm using the controller as syntax and it works just fine

$stateProvider.state('base.home', {
  templateUrl: "/templates/base/home.html",
  url: '/',
  controller: 'homeCtrl as home'
});

then it's just {{home.whatever}} in template

jasonals avatar Oct 12 '13 11:10 jasonals

Is there any way to use "controller as" syntax when you are passing in a controller as a function?

The end goal is to write something as follows, without having to create say, IndexView and IndexController

// about/controllers.js
module.exports.index = BaseController.extend({
  name: 'about',
  url: 'about',
  inject: ['$scope'],
  template: '<button ng-click=ctrl.something()>{{ ctrl.val }}</button>',

  val: 0,
  something: function () { this.val++; }
});

// initialize.js
var About = require('about/controllers');

application.config([
  '$stateProvider', '$urlRouterProvider',
  function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/');
    $stateProvider.state(About.index);
  }
]);

mikew avatar Jan 09 '14 20:01 mikew

@mikew How do you use it elsewhere in Angular?

nateabele avatar Jan 10 '14 01:01 nateabele

@jasonals Thanks!

maximkott avatar Aug 31 '14 09:08 maximkott

+1 ! want ' controllerAs ' option

ariesjia avatar Sep 16 '14 03:09 ariesjia

All kind of controller assignments are supported. See for yourself: https://github.com/angular-ui/ui-router/wiki#controllers

Way avatar Oct 10 '14 04:10 Way

I like the controller as syntax but as parent-child controllers are common, can a child controller set a watch on a parent controller variable without knowing the instance name of the controller? (I don't want to debate the need to use $watch in this instance, the question is probably relevant to other scenarios)

Trying to watch for changes in a parent 'active ID' from the child controller so I could retrieve the relevant child data for a child view, it seems I have to refer to the 'as name' i.e.

$scope.$parent.$watch( 'controllerasname.activeID' ) alternatively this works too: $scope.$watch( '$parent.controllerasname.activeID' )

So the child needs to know the name used for controller as, limiting its usefulness as a re-usable component?

I tried all other ways I could imagine without using the controllerasname and none of them correctly triggered the watch.

troywray avatar May 19 '15 12:05 troywray