4

I am learning Angular2 RC5 with Angular2 material libraries and getting it to run in a AspNetCore 1.0 app using VisualStudio 2015. I took a perfectly running Angular2 RC5 single page application and tried to introduce lazy loading where only the login page will load first. When I have successfully logged in, I want to load the remaining pages.

When I clicked on the login button in SPA mode, I would redirect successfully to the DashboardComponent page which was preloaded with the login page - that was before I introduced lazy loading. After following this tutorial I now get these errors

Error Snippet

EXCEPTION: Error: Uncaught (in promise): Template parse errors: Can't bind      to 'ngIf' since it isn't a known property of 'md-card-title'.
1. If 'md-card-title' is an Angular component and it has 'ngIf' input, then verify that it is part of this module.
2. If 'md-card-title' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.  ("


        <md-card-title [ERROR ->]*ngIf="!showMessage && !isApproved">
            Please use the information below...
        </md-card-title> "): ReportListComponent@63:23 Property binding ngIf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "directives" section. ("

Why is the ngIf directive suddenly not working? It was working before I moved the Dashboard to load lazily post the login success.

I have gone all the way till using the Shared modules approach in the link I posted above. Is not using the Shared modules a cause for this issue? I presumed module sharing to be a way to clean up the code and not repeat stuff once everything works and nothing more than that.

My Dashboard is similar to the HeroesList page in the link I am following above, where in it loads some more pages the minute the DashboardModule is loaded. I have put breakpoints in the Dashboard page on the MVC side and have confirmed that the Dashboard and pages after does load lazily only on a successful login.

The error seems like its not able to get the reference of core angular references and/or angular2 material libraries. I have put all of those imports listed here in all the modules that would have them other than the routing modules

import { NgModule }         from '@angular/core';
import { FormsModule } from '@angular/forms';

import { BrowserModule } from '@angular/platform-browser';
import { HttpModule }    from '@angular/http';
import { RouterModule } from '@angular/router';

import { MdRadioModule } from '@angular2-material/radio';
import { MdToolbarModule } from '@angular2-material/toolbar';
import { MdButtonModule } from '@angular2-material/button';
import { MdIconModule } from '@angular2-material/icon';
import { MdProgressBarModule } from '@angular2-material/progress-bar';
import { MdListModule } from '@angular2-material/list';
import { MdInputModule } from '@angular2-material/input';
import { MdCardModule } from '@angular2-material/card';

Still I get the errors as in the snippet above. What could I be missing? Any help or tips appreciated.

EDIT

Dashboard.Module.ts

import { NgModule }         from '@angular/core';
import { CommonModule }     from '@angular/common';
import { FormsModule } from '@angular/forms';


import { HttpModule }    from '@angular/http';
import { RouterModule } from '@angular/router';

import { MdRadioModule } from '@angular2-material/radio';
import { MdToolbarModule } from '@angular2-material/toolbar';
import { MdButtonModule } from '@angular2-material/button';
import { MdIconModule } from '@angular2-material/icon';
import { MdProgressBarModule } from '@angular2-material/progress-bar';
import { MdListModule } from '@angular2-material/list';
import { MdInputModule } from '@angular2-material/input';
import { MdCardModule } from '@angular2-material/card';

import { AppService } from './app.service';

import { DashboardRoutingModule }  from './Dashboard-routing.Module';

@NgModule({
    imports: [  
                CommonModule, HttpModule, FormsModule, RouterModule, 
                MdRadioModule, MdToolbarModule, MdProgressBarModule,
                MdButtonModule, MdIconModule, MdListModule,
                MdCardModule, MdInputModule, MdToolbarModule,
                DashboardRoutingModule],
  /*declarations: [  DashboardComponent ],*/
  exports:      [  CommonModule  ],
  providers:    [ AppService ]
})
export class DashboardModule { }

DashBoardComponent.ts

import { Component }   from '@angular/core';

import { AppService } from './app.service';

@Component({

  template: `
    <router-outlet></router-outlet>
  `
})
export class DashboardComponent {
  userName = '';
  constructor(service: AppService) {
    //this.userName = service.userName;
  }
}

dashboard-routing.Module.ts

import { NgModule }            from '@angular/core';
import { Routes, RouterModule }        from '@angular/router';

import { FormsModule } from '@angular/forms';
import { CommonModule }     from '@angular/common';
import { HttpModule }    from '@angular/http';

import { MdRadioModule } from '@angular2-material/radio';
import { MdToolbarModule } from '@angular2-material/toolbar';
import { MdButtonModule } from '@angular2-material/button';
import { MdIconModule } from '@angular2-material/icon';
import { MdProgressBarModule } from '@angular2-material/progress-bar';
import { MdListModule } from '@angular2-material/list';
import { MdInputModule } from '@angular2-material/input';
import { MdCardModule } from '@angular2-material/card';


import { DashboardComponent } from './DashBoardComponent';
import { ReportListComponent }  from './ReportListComponent';
import { ReportDetailComponent }  from './ReportDetailComponent';


const routes: Routes = [
  { 
    children: [
      { path: 'Home/ReportList',    component: ReportListComponent },
      { path: 'Home/ReportDetail/:reportId', component: ReportDetailComponent }
    ]
  }
];


@NgModule({
    imports: [RouterModule.forChild([routes])  
    ],
  declarations: [ DashboardComponent, ReportListComponent, ReportDetailComponent ],
  exports: [RouterModule, ReportListComponent, ReportDetailComponent, CommonModule]
})


export class DashboardRoutingModule {}
Splaktar
  • 5,506
  • 5
  • 43
  • 74
user20358
  • 14,182
  • 36
  • 114
  • 186
  • Did you try wrapping inside a
    containing the ng-if: `
    `
    – mika Oct 17 '16 at 22:30
  • I did and got this error `Can't bind to 'ngIf' since it isn't a known property of 'div'. ("` – user20358 Oct 17 '16 at 22:38
  • http://stackoverflow.com/questions/39058075/cant-bind-to-ngif-since-it-isnt-a-known-property-of-div – mika Oct 17 '16 at 23:18
  • Where is the `` included. Add the relative template to your post. – nlyk Oct 18 '16 at 05:41
  • What version of material are you using? It's better to upgrade to Angular 2.0 final and the latest material version (2.0.0-alpha.9-3) – nlyk Oct 18 '16 at 05:48
  • Using Material 2.0 alpha 8-2 – user20358 Oct 18 '16 at 14:30
  • @nlyk: The `` snippet is part of the ReportList page which is supported by the ReportList component.The Dashboard component holds the `router-module` tag to load the routes into – user20358 Oct 18 '16 at 14:55
  • @user20358 Similar error logs I have encounter when the Material module is not loaded. I'm not familiar with the version you are using. In my project I just include the MaterialModule in the import property of my module metadata. – nlyk Oct 18 '16 at 15:10
  • I've done that too. Atleast I think I have – user20358 Oct 18 '16 at 15:12
  • I can't even bind `ngIf`to a `
    – user20358 Oct 18 '16 at 15:31

2 Answers2

10

Dashboard component is now part of another module and you need to import CommonModule to that module in order to use *ngIf directive. You don't need to import CommonModule to your AppModule because you import BrowserModule there, and BrowserModule imports and then exports CommonModule, which means you get CommonModule automatically when you import BrowserModule.

Stefan Svrkota
  • 48,787
  • 9
  • 98
  • 87
  • Dashboard module or component? – user20358 Oct 17 '16 at 22:36
  • @user20358 You need to add `CommonModule` to `DashboardModule`'s imports. – Stefan Svrkota Oct 17 '16 at 22:39
  • @user20358 Can you please add `DashboardComponent` and `DashboardModule` code to your post? – Stefan Svrkota Oct 17 '16 at 22:41
  • ok. That will take a while as I have to clean out the irrelevant bits. Will do that asap. – user20358 Oct 17 '16 at 22:42
  • Done. Can you have a look please? – user20358 Oct 17 '16 at 22:52
  • @user20358 Why is your `DashboardComponent` declaration commented out? It should be declared there. – Stefan Svrkota Oct 17 '16 at 22:58
  • I also need the `DashboardComponent` in the routing module so that the login navigates to the DashBoard page. Updated question with the `dashboard-routing.module.ts` file. Without the dashboardRouting module the navigation doesn't happen. With the DashboardComponent not commented I get this error: `Uncaught (in promise): Type DashboardComponent is part of the declarations of 2 modules: DashboardRoutingModule and DashboardModule!` – user20358 Oct 18 '16 at 14:27
  • If you need to use one component in two modules, you can make a third module, let's call it `SharedModule` and then declare and EXPORT (this is a must!) `DashboardComponent` from `SharedModule`. After that, you need to import `SharedModule` to `DashboardRoutingModule` and `DashboardModule`. That's a solution for your problem. – Stefan Svrkota Oct 18 '16 at 15:31
  • 1
    oh. I thought shared module was only to keep things clean. Let me try that out and report back. Thanks Stefan :) – user20358 Oct 18 '16 at 15:48
  • well the `*ngIf="!showMessage && !isApproved"` issue got resolved, But now down the page I have another angular material design component that is still not going. `Can't bind to 'value' since it isn't a known property of 'md-progress-bar'.` `{{report.completedVal}} % ][value]="report.completedVal" style="width:90%;">` – user20358 Oct 18 '16 at 20:46
  • @user20358 Well, the problem you asked about in your question is solved, the error you are getting now has nothing to do with original problem. – Stefan Svrkota Oct 18 '16 at 20:50
  • actually that was just a snippet of the full error. basically all references to angular were not being recognized. That I presume was due to the modules not being loaded as they were not shared as you correctly pointed out. Now that the mandatory shared module has been done and referenced, I can't see why some angular directives work and some others don't. – user20358 Oct 18 '16 at 20:56
  • I import browsermodule already but still get the error 'cant bind ngif" – Kokodoko Nov 04 '16 at 15:12
1

Today i have encountered such an error with ionic 5 it was because i had deleted the router line that was in charge of this page cause it was a modal.. None of *ngIf nor *ngFor were working until I put there links back inside the app.routing file

  • 1
    This was exactly my experience. I added modules that were intended as modals, so I didn't think I needed the router lines. Everything was working until I tried to add *ngIf to the template. – smatthews1999 Feb 20 '21 at 19:08