er-sarjan / innovify-frontend-test

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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!

gulp watch doesn't start server so use http-server module (install from npm globally if not installed) at root folder of project and go localhost:8080/build/ and review project functionality

Note: If UI does not work properly, remove build folder and re-run gulp and check view again

License

MIT

About


Languages

Language:JavaScript 73.7%Language:HTML 19.3%Language:CSS 7.1%