Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controllerAs syntax name conflicts #577

Open
jusefb opened this issue Nov 5, 2015 · 9 comments
Open

controllerAs syntax name conflicts #577

jusefb opened this issue Nov 5, 2015 · 9 comments

Comments

@jusefb
Copy link

jusefb commented Nov 5, 2015

Hi,

Something I have discovered today with regards to the controllerAs syntax in Angualr 1. In my Angular 1 application I try to apply the component architecture by using directives to define individual components. The directive has its own controller with controllerAs: 'vm'. I also use angular-ui router for navigating between views. What I have discovered is that if I have a route defined with controllerAs: 'vm' and then I use a directive within the view that defines controllerAs: 'vm', the vm of the parent controller gets overwritten. The behaviour is the same even with isolated scope on the directive. If however I specify different names in the controllerAs parameter for router and the directive (ex controllerAs: 'componentVm') all works fine.

I was wondering if I am doing something wrong? If not and if this is the intended behaviour it would probably be a good idea to add something to the styleguide mentioning potential controllerAs name conflicts when using component architecture in Angular 1 with directive specific controllers.

Kind regards

Jusef

@kylecordes
Copy link

The style guide already says:

"Note: When working with larger codebases, using a more descriptive name can help ease cognitive overhead & searchability. Avoid overly verbose names that are cumbersome to type."

So I think @johnpapa has this covered well. Perhaps a slight improvement would be to mention that using nested controllers counts as "larger codebases".

(However... if you're building with well isolated components, you probably have few very explicitly nested controllers even in large projects.)

@zachlysobey
Copy link
Contributor

That section of the style-guide is quite new, and probably could be expanded/improved a bit IMO.
See this conversation for more info: #462

@johnpapa
Copy link
Owner

Feel free to make a PR to add a short bit on nesting controllers, if you think it wil help

drKnoxy added a commit to drKnoxy/angular-styleguide that referenced this issue Nov 25, 2015
Also i think the third bullet point helps cover the fact that you *can* use multiple names
johnpapa#577
johnpapa#462
@josefromsanjose
Copy link

I'm a bit new to Angular and just transition from angular 1.4.8 to 1.5.0. I had the same problem, when nesting a directive. The parent directive would controllerAs property as 'vm' was overwritten by the child directive. adding an isolate scope to the child fixed the issue for me, but I can see where these can cause a problem.

@johnpapa
Copy link
Owner

johnpapa commented Sep 5, 2016

Feel free to make a PR to add a short bit on nesting controllers, if you think it wil help

@karlhorky
Copy link

In my experience with naming Angular 1 controllers, out of the available options it makes the most sense to rename them after the component itself. This would only fall down with nesting of the same type of component inside one type, which in experience with a large app does not come up often.

@karlhorky
Copy link

So to clarify, I would suggest changing this rule in the style guide to recommend using the name of the component.

@johnpapa
Copy link
Owner

johnpapa commented Sep 5, 2016

Use vm until it's not clear. Then use a more descriptive name.
That's what I like.

@paulashton
Copy link

I had the same issue as the one jusefb reported in November 2015. We had changed some directives to be controllerAs: 'vm'. In one case a controllerAs: 'vm' did not have an isolated scope (but in all other cases the controllerAs: 'vm' directives did have an isoloted scope). Adding scope: {}, to the one controllerAs: 'vm' directive that didn't have an isolated scope fixed the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants