项目作者: seeden

项目描述 :
Angular ES6 utility library. Write directives, controllers and services as ES6 classes.
高级语言: JavaScript
项目地址: git://github.com/seeden/angular-es6.git
创建时间: 2015-03-16T09:45:22Z
项目社区:https://github.com/seeden/angular-es6

开源协议:MIT License

下载


Angular ES6 utility library. Write directives, controllers and services as ES6 classes.

What you will get

  • Write directives, controllers and services like an ES6 classes
  • Autoload directives, controllers, services, filters and factories with webpack

Babel

Use version 2.x for the babel 5 and version 3.x for the babel 6

Instalation

  1. npm install angular-es6

Examples

You can find whole example project in the example directory.

Directive

  1. export default class NiceDirective {
  2. static $inject = ['$http'];
  3. constructor($http) {
  4. this.$http = $http;
  5. this.template = '<div>{{computeName('NICE')}}</div>';
  6. this.restrict = 'E';
  7. this.scope = {
  8. name: '=',
  9. };
  10. }
  11. link(scope) {
  12. this.scope = scope;
  13. scope.computeName = (suffix) => this.computeName(suffix);
  14. }
  15. computeName(suffix = '') {
  16. const { $http, scope } = this;
  17. return 'Mr.' + scope.name + ' ' + suffix;
  18. }
  19. }

Controller

  1. export default class MainController {
  2. static $inject = ['$scope', '$http'];
  3. constructor($scope, $http) {
  4. this.$http = $http;
  5. $scope.doThis = () => this.doThis();
  6. }
  7. doThis() {
  8. const { $http } = this;
  9. ...
  10. }
  11. }

Class Inject

As you can see in the examples above. You need to store injected objects somehow.
There is a better solution. You can extend your class with class named Inject and then you can use variable named this.$inject.

In next example is called function doThis from the constructor.
You can use this.$inject because this object was initialized by Inject constructor.

Do not forget to use (…args). Inject class need to get all injected objects.

  1. import { Inject } from 'angular-es6';
  2. export default class MainController extends Inject {
  3. static $inject = ['$http'];
  4. constructor(...args) {
  5. super(...args);
  6. this.doThis();
  7. }
  8. doThis() {
  9. const { $http } = this.$inject;
  10. }
  11. }

Directive auto Inject

You can use variables from the link function anywhere in your directive code.
This feature is available without extending Inject class. Here is small example

  1. export default class NiceDirective {
  2. static $inject = ['$http'];
  3. constructor($http) {
  4. this.$http = $http;
  5. this.template = '<div>{{computeName('NICE')}}</div>';
  6. this.restrict = 'E';
  7. this.scope = {
  8. name: '=',
  9. };
  10. }
  11. link(scope) {
  12. scope.computeName = (suffix) => this.computeName(suffix);
  13. }
  14. computeName(suffix = '') {
  15. const { scope, element } = this.link.$inject;
  16. element.text('Mr.' + scope.name + ' ' + suffix);
  17. }
  18. }

Auto load directives

Each directory need to have file index.js with content like this:

  1. import { load } from 'angular-es6';
  2. const MODULE_NAME = 'myProject.directives';
  3. load.directives(require.context('./', true, /.*\.js$/), MODULE_NAME);
  4. export default MODULE_NAME;

More examples you can find in the example directory.

Pull request

Pull requests are welcome

Run build for production

  1. npm run build

Run build for development

  1. npm run dev