-
Notifications
You must be signed in to change notification settings - Fork 27.4k
REQUEST FOR FEEDBACK: angular.component()
- default directive controller name
#13664
Comments
c) programmers are tempted to use |
@drpicox - I am tempted to say that we ban |
I agree, after considering this:
But I consider that the second case is better to do with directives, decorators are not frequent, usually low level, and not suited for juniors. So probably is good idea to ban In other track of thinking, about decorators, a function to get the controller of the 'E' component of the current entity should be good idea, specially to write generic decorators to deal with any current component (require forces to know in advance which controller is and you cannot use a kind of interface of what are you looking for). |
I like |
I like |
different suggestion: why not have the name of the component as the controller instance name? |
@tabanliviu That's how it's currently done on |
@mgol 👍 I think I glossed over that when I read the ticket. Should this change be considered more of a ngUpgrade issue? I think in the context of angular 1.x the current implementation is a good solution. Perhaps exposing this as a settings function that takes the component name and outputs the controller name? this way it serves the current pattern and a future migration path. |
Bikeshed time. +1 for $ctrl. Ctrl has lots of pre-existing culture in Angular 1 documentation and examples as the "controller" suffix. Try googling "angular ctrl" for good measure. The current choice of deriving it from component name is quite nasty, as they indeed tend to get pretty long in real applications. |
angular.component()
- default directive controller name
👍 for |
+1 for We debated this pretty heavily on the ng-forward team and decided that |
I vote for Oh, I'd also like to add that
Nah. Not really. |
I think that would be most comprehensible and intuitive for everyone. |
+1 Other suggestions:
|
I vote for |
+1 for $ctrl |
I also prefer the +1 for $ctrl |
+1 for $this I would even go with |
I would go for $ctrl or $cc (being short for component controller) |
+1 for $ctrl |
We could call it just |
I am not a fan of confusing names. I think I vote for either |
How about |
+1 $comp |
@petebacondarwin Oh the amount of dyslectics (like myself) who will bomb us with issues about this... :) @drpicox Thanks for the explanation, I see your points and they are valid. it's a tough one, but I can share that at least from my experience, I had no trouble teaching developers the "vm" convention, and helped a few companies structure their massive app that way, they got it pretty fast, but maybe I'm alone in this experience. But I understand your points. agree with $ I'm still for $vm though, but am fine with $comp as well... |
@wesleycho from the Angular UI Bootstrap team seems to be strongly against |
+1 for |
@shairez I share completely your point about having a convention, I'm a Freelance Architect with tens of projects behind, the |
So the votes are in and it looks like this:
The clear favourite is |
Awesome, $ctrl it is! Issue for 1.4 and lower - can't name an 'as name' with
|
perhaps migrate in phases: Another possible way - although verbose - is to generate controllerAs alias in runtime, checking angular
.module('github')
.directive('issueThread', issueThread);
/* @ngInject */
function issueThread () {
// this can be required as a module if using some module loader
// or - another way is using global on angular namespace (i know it a bad practice - hwoever just to indicate reuse of this check
let prefix = angular.version.minor === 5 ? '$' : '';
let controllerAs = prefix + 'ctrl';
// with template strings
var controllerAs = `${prefix}ctrl`;
var directive = {
controller: controller,
restrict: 'E'
};
return directive;
function controller() {
}
} |
@drpicox you got a point there :). |
Why not just use |
(If anyone wants to have a shot at updating that indentifier extracting RegExp, it's right there btw.) |
@gkalpak that's a good point, moving forward promoting the use of the But, I think it might be confusing if we'll start teaching people about So a forward compatibility (not sure how), is a great idea! |
@shairez did you (can you) create a new issue to track this? |
I created #13736 which allows That said, I am not sure that promoting |
Thanks @gkalpak - I agree that we should probably encourage use of the |
One thing is: documentation of component says: "Component definitions are very simple and do not require much of the complexity behind defining general directives". |
@frfancha - the performance improvement is due to not having to use the The idea of the component helper is to make it simpler (in LOC sense) to write component type (isolate, element) directives; and easier to write code that is more inline with how things are done in Angular 2. If there is a performance issue in a specific app then it would be fairly straightforward to convert a component directive over to use the more general I think we need to take a look at the other API docs and developer guides to ensure they are consistent with the new |
As discussed in #13664 (comment). Closes #13736
@petebacondarwin At the beginning we wrote all our directives with controller, just because it was shown this way in the first tutorial we followed. |
thanks @gkalpak ! @petebacondarwin a separate issue isn't relevant anymore right? (Because of the PR) I agree (as I wrote), we should educate people to use the controllerAs property, but just mentioned it because I predict people will run into it. |
As discussed in #13664 (comment). Closes #13736
We should use a consistent default value for the name of a component's directive controller when it is attached to the scope. See #10007 (comment)
Currently we are defaulting to the canonical name of the component. This is not ideal as
a) component names can become long and unwieldy for use in a template
b) it is more complicated to automatically update the template to be used in Angular 2, where the context is the controller.
The criteria for the name are:
$
In addition the name should represent what is actually being published to the scope.
Some of previous suggestions include:
vm
- this is the commonly used name in many applications but the controller is not necessarily a "view model"$comp
- this is the current suggestion from the team but can be confused with compare and is not that short$ctrl
- this can be confused with input ConTRoL elements$this
- the controller is not reallythis
in the template, since the context is still actually the scopeThe text was updated successfully, but these errors were encountered: