angular-custom-controller-as

0.0.2 • Public • Published

Angular 1.5 introduced the .component() helper method. Its usage allows developers to write in an Angular 2 style as well, which will in turn make upgrading to Angular 2 an easier feat.

Simple example of .component() usage is next

.component('example', {
  ...
  controller: 'SomeCtrl as something',
  ...
});

controllerAs property was added to maintain backwards compatibility or keep it if that’s within your style for writing Directives/Components.

.component('example', {
  ...
  controller: 'SomeCtrl',
  controllerAs: 'something'
  ...
});

Default value of controllerAs property is $ctrl. If you use another keyword in your codebase and want to migrate to componets this tiny module/script should help you. Just load and use it

angular.setControllerAsDefaultValue('blah');

and you will be able to access controller properties in template without adding controllerAs option to definition of all components

.component('example', {
  ...
  bindings: {
    name: '@'
  },
  controller: 'SomeCtrl',
  template: `
    <div>{{ blah.name }}</div>
  `
  ...
});

This is achieved by help of trick that is described in Alex Peattie's post in his blog.

If you want to preserve old .module() method you have two options here

angular.setControllerAsDefaultValue('blah', true); // will be saved under `moduleOrig`
// typeof angular.moduleOrig -> 'function'

// or 
angular.setControllerAsDefaultValue('blah', 'oldModuleFn');
// typeof angular.oldModuleFn -> 'function'

Package Sidebar

Install

npm i angular-custom-controller-as

Weekly Downloads

2

Version

0.0.2

License

MIT

Last publish

Collaborators

  • zubb