Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ES6 modules syntax with Angular 1.x.x #11454

@nervgh

Description

@nervgh

Hi!

I use Babel for writing es6 code. It looks like this:

import Bar from 'path';

class Foo extends Bar {
    constructor() {
        super();
    }
}

export default Foo;

When I write angular module, I must wrap my class into function to make it work:

// ----class----
export default function __(Bar) {

    class Foo extends Bar {
        constructor() {
            super();
        }
    }

    return Foo;
}

__.$inject = [
    'Bar'
];

// ----module----
import Foo from 'path';

angular
    .module('test', [])
    .factory('Foo', Foo);

I thought I found solution without wrapping, but it not works with extends (sandbox):

function Foo() {
    Foo.__['$rootScope'].x = 1;
}

Foo.__ = function($rootScope) {
    Foo.__['$rootScope'] = $rootScope;
    return Foo;
};

Foo.__.$inject = [
    '$rootScope'
];


angular
    .module('test', [])
    .factory('Foo', Foo.__)
    .run([
        'Foo',
        function(Foo) {
            console.log(new Foo());
        }
    ])

I tried to use angular.injector() but in this case the view not updates (sandbox):

var ng = angular.injector(['ng']);
var $rootScope = ng.get('$rootScope');

function Foo() {
    console.log('Foo#constructor');
    $rootScope.x = 1;
    // $rootScope.$apply();
}

Foo.__ = function() {
    console.log('Foo.__');
    return Foo;
};

angular
    .module('test', [])
    .factory('Foo', Foo.__)
    .run([
        'Foo',
        function(Foo) {
            console.log(new Foo());
        }
    ])

Is there way write angular code without wrapping?

Thanks =)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions