question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

alpha-38: Need @Inject to inject ElementRef and DynamicComponentLoader

See original GitHub issue

DI works fine in alpha-37, now it needs @Inject.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:19 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
mgechevcommented, Oct 3, 2015

I started debugging your issue and found that MyApp gets transpiled to:

// ...
var MyApp = (function () {
    function MyApp(elementRef) {
        console.log(elementRef);
    }
    MyApp = __decorate([
        angular2_1.Component({
            selector: 'my-app'
        }),
        angular2_1.View({
            template: "<h1>hello</h1>"
        }), 
        __metadata('design:paramtypes', [Object])
    ], MyApp);
    return MyApp;
})();
angular2_1.bootstrap(MyApp);

Which means that the generated metadata for the param types is wrong. However, if you use @Inject instead of Object you get angular2_1.ElementRef, which is correct.

Same error occurs when I try to inject DynamicComponentLoader.

Another thing I noticed: when using angular2-seed with gulp task with the following src, I get the error. However, if I use target ../app/*.ts instead everything works.

Another way to make it work is by changing the name of the export inside the angular2 bundle:

  var element_ref_1 = require("angular2/src/core/linker/element_ref");
  exports.ElementRef2 = element_ref_1.ElementRef;

and later import it as:

import {ElementRef2...} from 'angular2/angular2';

PS: I dug inside the TypeScript compiler and saw that during the parameters serialization when writing metadata the ElementRef class is not being properly resolved; instead of TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue it is being resolved as TypeReferenceSerializationKind.ObjectType, so may be a TypeScript issue?

0reactions
angular-automatic-lock-bot[bot]commented, Sep 7, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic component loader - Angular
An application might need to load new components at runtime. This cookbook shows you how to add components dynamically. See the live example...
Read more >
Angular 2 dynamic component injection and access to ...
I want to inject the dynamic components inside the datalist and be able to access the attribute suggestion search.result.component afterwards ...
Read more >
Loading Components Dynamically in an Angular App
A tutorial that shows how to create an Angular app with dynamic component loading.
Read more >
Loading Components Dynamically in Angular - SyntaxSuccess
In the following article I will show how to use ComponentFactoryResolver in Angular to insert components dynamically into the DOM.
Read more >
Loading Components Dynamically in Angular 9 with Ivy
This is not exactly new and exclusive to Angular 9, but now we can have components without a module, and by making them...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found