ngUpgraders / ng-forward

The default solution for those that want to write Angular 2.x style code in Angular 1.x
411 stars 36 forks source link

Support using angular2 component into ng-forward component #120

Closed david-gang closed 4 years ago

david-gang commented 8 years ago

Hi all,

I try to use ng-upgrade with a small directive, to see how it works.

        'use strict';
        import {Component} from 'angular2/core';

        @Component({
            "selector": "logo",
            templateUrl: 'components/logo/logo.html',
        })

        export class Logo {
        }

Now i try to inject it into another directive:


    'use strict';
    import {Component} from 'ng-forward';
    import {Logo} from './logo/logo.directive';
    import {getAdapter} from '../../angular_upgrade'; //my wrapper to get an adapter singleton
//
    @Component({
       directives:[Logo],
        providers: [],
        "selector": "topbar",
        templateUrl: 'components/topbar/topbar.html',
    })

    export class Topbar {
    }

Now i get an error

Uncaught TypeError: TypeError while analyzing Component 'Topbar' directives. Invalid Providers: please make sure all providers are an Injectable(), Component(), Directive(), a >Provider, or a module string. Here's the invalid values: function Logo() { }

When i try to downgrade the component

directives:[getAdapter().downgradeNg2Component(Logo)],

i get an error:

Uncaught TypeError: TypeError while analyzing Component 'Topbar' directives. Invalid Providers: please make sure all providers are an Injectable(), Component(), Directive(), a > Provider, or a module string. Here's the invalid values: function directiveFactory(protoViewRefMap, viewManager, parse) { var protoView = protoViewRefMap[info.selector]; if (!protoView) throw new Error('Expecting ProtoViewRef for: ' + info.selector); var idCount = 0; return { restrict: 'E', require: constants_1.REQUIRE_INJECTOR, link: { post: function (scope, element, attrs, parentInjector, transclude) { var domElement = element[0]; var facade = new downgrade_ng2_adapter_1.DowngradeNg2ComponentAdapter(idPrefix + (idCount++), info, element, attrs, scope, parentInjector, parse, viewManager, protoView); facade.setupInputs(); facade.bootstrapNg2(); facade.projectContent(); facade.setupOutputs(); facade.registerCleanup(); } } }; }

What is wrong here?

david-gang commented 8 years ago

The problem is that

_writers.providerStore.has('name', a)

The function is not in the provider store as it is not part of ng-forward or a ng1 module.

Is there a nice way to support this?

I could theoretically register this as a ng1 directive with a string and inject the string but this is really ugly.

david-gang commented 8 years ago

In a more general way i would like to know how to inject from angular2 to ng-forward and vice versa meaning:

timkindberg commented 8 years ago

@david-gang this is some good research! Thanks for doing this.

So the ng2 component was successfully downgraded, we can see the ng1 DDO that the adapter created. We just need to figure out how to get that output over to something that ng-forward wants which I'm not sure if that is possible but it certainly looks like it might be. However, that is gonna be a little while.

Both @MikeRyan52 and I have been busy with other things for the last 2 weeks that's why there's been no movement in a bit. I plan to start working on ng-forward again maybe around my holiday break and certainly after the new year.