• Stars
    star
    370
  • Rank 115,405 (Top 3 %)
  • Language
    JavaScript
  • Created almost 10 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An experiment in using ES6 features with AngularJS 1.x

Angular ES6

An example approach to using ES6 classes in an AngularJS 1.x app.

Please see the article Exploring ES6 Classes In AngularJS 1.x for a full explanation.

Update, October 2015: The material in this repo has been developed even further by others. If you want to write Angular 1.x apps in the Angular 2 style, please see the ng-forward project, which unlike this repo, is under active development.

Working demo here

register.js

The style of class definition you see below is enabled by including the file register.js in the project, which exposes the global function register

The API is as follows:

class MyAngularComponent {
    // ...
}

register(appName)
    .controller('MyController', MyAngularComponent)
    .service('myService', MyAngularComponent)
    .provider('myOtherService', MyAngularComponent)
    .factory('myFactory', MyAngularComponent)
    .directive('myDirective', MyAngularComponent);

Example Component Classes

Service

class UserService {
    /*@ngInject*/
    constructor($http) {
        this.$http = $http;
    }
    getFullName() {
        return this.$http.get('api/user/details');
    }
}

register('app').service('userService', UserService);

Controller

class MyController {
    /*@ngInject*/
    constructor(userService) {
        userService.getFullName()
            .then(result => this.userName = result.fullName);
    }
}

register('app').controller('MyController', MyController);

Factory

class ThingFactory {
    /*@ngInject*/
    constructor($timeout) {
        this.$timeout = $timeout;
    }
    newThing() {
        console.log('Getting a new Thing...');
        return this.$timeout(() => new Thing(), 1000);
    }
}

register('app').factory('thingFactory', ThingFactory);

Directive

class MyDirective {
    /*@ngInject*/
    constructor($interval) {
        this.template = '<div>I\'m a directive!</div>';
        this.restrict = 'E';
        this.scope = {}
        // etc. for the usual config options

        // allows us to use the injected dependencies
        // elsewhere in the directive (e.g. compile or link function)
        this.$interval = $interval;
    }

    // optional compile function
    compile(tElement) {
        tElement.css('position', 'absolute');
    }

    // optional link function
    link(scope, element) {
        this.$interval(() => this.move(element), 1000);
    }

    move(element) {
        element.css('left', (Math.random() * 500) + 'px');
        element.css('top', (Math.random() * 500) + 'px');
    }
}

register('app').directive('myDirective', MyDirective);

Provider

class ThingServiceProvider {
    constructor() {
        this.apiPath = 'default/api';
    }
    setApiPath(value) {
        this.apiPath = value;
    }
    /*@ngInject*/
    $get($http) {
        return {
            getThings: () => $http.get(this.apiPath)
        };
    }
}

register('app').provider('thingService', ThingServiceProvider);

Build

Clone this repo and then npm install and bower install to download the required dependencies.

Then gulp watch and start hacking!

License

MIT

More Repositories

1

angularUtils

A place where I will collect useful re-usable Angular components that I make
JavaScript
1,999
star
2

ngx-pagination

Pagination for Angular
TypeScript
1,213
star
3

soundcloud-visualizer

Audio visualization with web audio API, canvas & the SoundCloud API
HTML
449
star
4

chromata

A generative art tool.
JavaScript
376
star
5

angular-wordpress-seed

A bare-bones AngularJS blog app designed to work with the Wordpress JSON REST API.
JavaScript
310
star
6

horizonal

Turn your static HTML document into a programmable slide show
JavaScript
205
star
7

css-space-shooter

An old-school arcade-style 3D shoot-em-up rendered entirely with CSS 3D transforms
JavaScript
176
star
8

skqw

JavaScript Audio Visualizer
TypeScript
120
star
9

angular-social-demo

A demo of how to allow social media crawlers (Facebook, Twitter, Pinterest etc) to read your AngularJS app
HTML
110
star
10

angularUtils-pagination

Simple pagination for AngularJS
JavaScript
42
star
11

drawACatApp

A canvas line drawing experiment
JavaScript
29
star
12

heavenly-glory

A kung-fu movie starring you. Web RTC & Web Audio experiment.
JavaScript
23
star
13

css-experiments

Some experiments involving CSS
CSS
14
star
14

michaelbromley.co.uk

The source code for my personal website and blog
JavaScript
13
star
15

headlesscommerceplatforms

A feature comparison of various headless e-commerce solutions
TypeScript
10
star
16

shader-playground

Some snippets of code for fragment shaders
GLSL
9
star
17

cmpg

A super simple component generator for Angular 2
JavaScript
9
star
18

breezejs-odata4-adapter

An experimental adapter to allow BreezeJS to work with an OData v4 server.
JavaScript
7
star
19

skqw-library

Visualization library for SKQW
JavaScript
5
star
20

angularUtils-uiBreadcrumbs

Automatic breadcrumbs directive for use with angular-ui-router
JavaScript
3
star
21

vendure-plugin-example

Handlebars
3
star
22

canvas_experiments

Simple examples of HTML canvas
JavaScript
3
star
23

vendure-demo

TypeScript
2
star
24

angular-issue-18478

Reproduction of Angular issue 18478
TypeScript
1
star
25

pad

A new way to work with text.
TypeScript
1
star
26

angularUtils-disqus

A directive for including Disqus comments in an AngularJS app
JavaScript
1
star
27

gulp-es6-seed

A minimal seed to kick start an ES6 project
JavaScript
1
star