EmmanuelDemey / EmmanuelDemey/eslint-plugin-angular
Assigning Controllers (Y038)
- Dominant language
- JavaScript
- Stars
- 620
- Forks
- 127
- PR merge metrics
- No merged PRs in 30d
Description
https://github.com/johnpapa/angularjs-styleguide/edit/master/README.md
- When a controller must be paired with a view and either component may be re-used by other controllers or views, define controllers along with their routes.
Note: If a View is loaded via another means besides a route, then use the `ng-controller="Avengers as vm"` syntax.
_Why?_: Pairing the controller in the route allows different routes to invoke different pairs of controllers and views. When controllers are assigned in the view using [`ng-controller`](https://docs.angularjs.org/api/ng/directive/ngController), that view is always associated with the same controller.
``` javascript
/* avoid - when using with a route and dynamic pairing is desired */
// route-config.js
angular
.module('app')
.config(config);
function config($routeProvider) {
$routeProvider
.when('/avengers', {
templateUrl: 'avengers.html'
});
}
```
``` html
```
``` javascript
/* recommended */
// route-config.js
angular
.module('app')
.config(config);
function config($routeProvider) {
$routeProvider
.when('/avengers', {
templateUrl: 'avengers.html',
controller: 'Avengers',
controllerAs: 'vm'
});
}
```
``` html
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.