1

In my Angular-cli RC5 project I am trying to create charts.

  1. Created a new component as "chart"
  2. Created a directive as "line-graph.directive.ts"

Folder structure.

enter image description here

app.module.ts

import {NgModule, enableProdMode}       from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {AppComponent}   from './app.component';
import {ChartComponent} from './chart/chart.component';
import {LineGraphDirective} from './chart/line-graph.directive';

@NgModule({
   declarations: [AppComponent, ChartComponent, LineGraphDirective],
   providers: [],
   imports: [],
bootstrap: [AppComponent],
})
export class AppModule {
}

chart.component.html

<p>
 chart works!
</p>

Then, I added following inside the app.component.html as below.

<app-chart></app-chart>

Then "chart works!" line should display when I run the application.

But it doesn't.

chart.component.ts

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

@Component({
 moduleId: module.id,
 selector: 'app-chart',
 templateUrl: 'chart.component.html',
 styleUrls: ['chart.component.css'],
 directives: []
})
export class ChartComponent {
  constructor() {}
}

line-graph.directive.ts

import {Directive, ElementRef} from '@angular/core';

@Directive({
  selector: 'line-graph'
})

export class LineGraphDirective {
  constructor() {}
}

app.component.ts

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

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})
export class AppComponent {
  title = 'app works!';
}

Any suggestions of what i have done wrong with my code.

Thank You

8
  • what you are getting as output ? are you getting any error ? Commented Sep 5, 2016 at 13:08
  • There's no any error. The page shows only the html content of app.component.html file. Commented Sep 5, 2016 at 13:10
  • From your folder structure your both chart component import path looks confusing. whats the code of ChartComponent and LineGraphDirective? Commented Sep 5, 2016 at 13:12
  • @ranakrunal9, I have posted the code for ChartComponent and LineGraphDirective. chart directory & app.module.ts are inside the app folder and line-graph-directive.ts file is inside the chart directory. Commented Sep 5, 2016 at 13:40
  • post your app.component.ts code Commented Sep 7, 2016 at 10:49

3 Answers 3

1

If you look in your browser's network requests, I bet you will find some 404 not found errors when Angular tries to load the chart component.

Your import paths are not correct. Change

import {ChartComponent} from './chart/chart.component';
import {LineGraphDirective} from './chart/line-graph.directive';

With the right paths. The folder structure is difficult for me to make out from your image (small indentation) but your current import would only work if the chart directory was in the same folder as AppModule

Sign up to request clarification or add additional context in comments.

3 Comments

There's no any 404 not found error. chart directory and app.module are inside app folder
@Rose18 ok. The structure is tough for me to see. If you inline the template (put it in template: '<p>Chart works!</p> instead of using templateUrl), does it work?
I tried by adding inline template to ChartComponent, it doesn't work either :(
0

You also have to import your chart component in the app.component.ts . Just add

import {ChartComponent} from './chart/chart.component';

It will definitely work

Comments

0

You need to also declare your Component at the bootstrap metadata in the app.module.ts. Without it, it won't display the content of your Component. I have also encountered this problem just a few hours ago.

So your app.module.ts should look like this:

import {NgModule, enableProdMode}       from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {AppComponent}   from './app.component';
import {ChartComponent} from './chart/chart.component';
import {LineGraphDirective} from './chart/line-graph.directive';

@NgModule({
   declarations: [AppComponent, ChartComponent, LineGraphDirective],
   providers: [],
   imports: [],
bootstrap: [AppComponent,ChartComponent],
})
export class AppModule {
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.