Skip to content

[ivy] ERROR Error: Template error: Can't bind to 'ngForOf' since it isn't a known property of 'div'. #32078

Closed

Description

@fr0

🐞 bug report

Affected Package

Ivy

Is this a regression?

Yes, works in non-ivy.

Description

May be related to #31994, however, supposedly that was fixed in 9.0.0-next.1, so I'm reporting it here again in case it is a different problem.

Here's what I've discovered:

  1. This problem only appears when an ng-packagr library is used.
  2. This problem goes away if --prod --aot is used (however then you will run into [ivy] app doesn't work when ng-packagr library is used #32080)
  3. Doesn't happen if enableIvy is set to false.

🔬 Minimal Reproduction

https://github.com/fr0/angular-test/tree/ivy

This repo contains the code for both the ng-packagr library and the app.

🔥 Exception or Error




core.js:4815 ERROR Error: Template error: Can't bind to 'ngForOf' since it isn't a known property of 'div'.
    at createUnknownPropertyError (core.js:10180)
    at elementPropertyInternal (core.js:10046)
    at Module.ɵɵproperty (core.js:16508)
    at ItemsComponent_Template (angular-test.js:44)
    at executeTemplate (core.js:9597)
    at checkView (core.js:11020)
    at componentRefresh (core.js:10778)
    at refreshChildComponents (core.js:9303)
    at refreshDescendantViews (core.js:9202)
    at checkView (core.js:11021)

🌍 Your Environment

Angular Version:




Angular CLI: 8.3.0-next.1
Node: 12.6.0
OS: darwin x64
Angular: 9.0.0-next.1
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.803.0-next.1
@angular-devkit/build-angular     0.803.0-next.1
@angular-devkit/build-optimizer   0.803.0-next.1
@angular-devkit/build-webpack     0.803.0-next.1
@angular-devkit/core              8.3.0-next.1
@angular-devkit/schematics        8.3.0-next.1
@angular/cli                      8.3.0-next.1
@ngtools/webpack                  8.3.0-next.1
@schematics/angular               8.3.0-next.1
@schematics/update                0.803.0-next.1
ng-packagr                        5.4.3
rxjs                              6.5.2
typescript                        3.5.3
webpack                           4.38.0

Activity

  1. added this to the needsTriage milestone on Aug 9, 2019
  2. JoostK commented on Aug 10, 2019

    @JoostK
    Member

    I am seeing something very strange here. When compiling with the CLI, the following code is generated into vendor-es2015.js:

    ItemsComponent.ngComponentDef = _angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵɵdefineComponent"]({ type: ItemsComponent, selectors: [["app-items"]], factory: function ItemsComponent_Factory(t) { return new (t || ItemsComponent)(_angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵɵdirectiveInject"](_angular_core__WEBPACK_IMPORTED_MODULE_1__["ChangeDetectorRef"])); }, inputs: { count: "count" }, features: [_angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵɵNgOnChangesFeature"]()], consts: 2, vars: 2, template: function ItemsComponent_Template(rf, ctx) { if (rf & 1) {
            _angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵɵelementStart"](0, "div", _c0);
            _angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵɵtemplate"](1, ItemsComponent_div_1_Template, 2, 1, "div", _c1);
            _angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵɵelementEnd"]();
        } if (rf & 2) {
            _angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵɵselect"](1);
            _angular_core__WEBPACK_IMPORTED_MODULE_1__["ɵɵproperty"]("ngForOf", ctx.items)("ngForTrackBy", ctx.trackByFn);
        } }, encapsulation: 2, changeDetection: 0 });

    However, just running ngc inside the repo results in the following:

    ItemsComponent.ngComponentDef = i0.ɵɵdefineComponent({ type: ItemsComponent, selectors: [["app-items"]], factory: function ItemsComponent_Factory(t) { return new (t || ItemsComponent)(i0.ɵɵdirectiveInject(i0.ChangeDetectorRef)); }, inputs: { count: "count" }, features: [i0.ɵɵNgOnChangesFeature()], consts: 2, vars: 2, template: function ItemsComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵelementStart(0, "div", _c0);
            i0.ɵɵtemplate(1, ItemsComponent_div_1_Template, 2, 1, "div", _c1);
            i0.ɵɵelementEnd();
        } if (rf & 2) {
            i0.ɵɵselect(1);
            i0.ɵɵproperty("ngForOf", ctx.items)("ngForTrackBy", ctx.trackByFn);
        } }, directives: [i1.NgForOf], encapsulation: 2, changeDetection: 0 });

    Note that the ngc output contains directives: [i1.NgForOf], which is missing from the code inside of vendor-es2015.js when using the CLI. This piece of generated code would have enabled the component to use the NgForOf directive at runtime, so it being elided will prevent it from being used at runtime.

    I have no clue why all of the directives get elided. The Angular compiler does generate the directives: [i1.NgForOf] code regardless of ngc vs CLI, and the emitted source file emitted from ngtsc does also contain that piece of code. So, it appears as if some transform later in the pipeline elides it from the code.


    When the code inside ./src/lib is moved under ./src/app/lib and the import of ComponentsModule is changed into ./lib/components.module instead of angular-test, the code is generated properly into main-es2015.js. So this being treated as a library does affect the final bundle code.


    EDIT: All of the above is not really relevant, as apparently angular-test is actually installed into node_modules and processed by ngcc 🤦‍♂

  3. JoostK commented on Aug 11, 2019

    @JoostK
    Member

    So, the bug is with ngcc, where the ItemsComponent's metadata has a declaration node of class ItemsComponent {} (the inner class declaration), whereas the component's scope (i.e. its registration in ComponentsModule) is registered under let ItemsComponent = class ItemsComponent {} (the outer class declaration). Therefore, the scope of the component can't be determined which results in the directives: [i1.NgForOf] to be missing from the compiled code.

  4. fr0 commented on Aug 28, 2019

    @fr0
    Author
  5. modified the milestones: needsTriage, Backlog on Sep 5, 2019
  6. angular-automatic-lock-bot commented on Oct 13, 2019

    @angular-automatic-lock-bot

    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.

  7. locked and limited conversation to collaborators on Oct 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions