Skip to content

Instantly share code, notes, and snippets.

@manekinekko
Created January 25, 2017 14:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manekinekko/935bb51b677fca6198256df9484597da to your computer and use it in GitHub Desktop.
Save manekinekko/935bb51b677fca6198256df9484597da to your computer and use it in GitHub Desktop.
Writing AngularJS (>=1.5) component using Angular (>=2) approach with TypeScript...

If you want to write an AngularJs (1.5) component using Angular (>=2) approach, you could use:

In app.module.ts

import { FooComponent } from './foo.component';

angular.module('app.module', [])
  .component('fooComponent', FooComponent);

In foo.component.ts

import { Component } from './core/helpers/decorators';

@Component({
  bindings: {
    bar: '<'
  },
  template: '<p>{{$ctrl.bar}}</p>'
})
export class FooComponent {

   bar: string;

   constructor(public $http: ng.IHttpService) {}

   $onInit() {
     // do something with this.bar or this.$http upon initialization
   }
}

In core/helpers/decorators.ts

export const Component = function(options: ng.IComponentOptions): Function {
  return (controller: Function) => {
    return angular.extend(options, { controller });
  };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment