Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 component not showing up

Tags:

html

angular6

I just started learning Angular yesterday so I apologize if I'm missing something obvious, but I am trying to display a component on the app.component.html, however, it is not showing up.

TS file for the component I am trying to display:

import { Component, OnInit } from '@angular/core';
import { ImageService } from '../shared/image.service';

@Component({
  selector: 'image-list',
  templateUrl: './image-list.component.html',
  styleUrls: ['./image-list.component.css']
})
export class ImageListComponent implements OnInit {

  images: any[];

  constructor(private _imageService : ImageService ) { }

  searchImages(query : string)
  {
    return this._imageService.getImage(query).subscribe
    (
      data => console.log(data),
      error => console.log(error),
      () => console.log("Request Completed!")
    );
  }

  ngOnInit() {
  } 
}

image-list.component.html :

<button>Find Images</button>

app.component.html :

<image-list></image-list>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

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

image.service.ts

import { Injectable } from "@angular/core";
import { environment } from "../../environments/environment";
import { Http, Headers } from "@angular/http";
import { map, filter, scan } from 'rxjs/operators';

@Injectable()
export class ImageService
{
    private query: string;
    private API_KEY: string = environment.API_KEY;
    private API_URL: string = environment.API_URL;
    private URL: string = this.API_URL + this.API_KEY + '&q=';

    constructor(private _http: Http) {

    }

    getImage(query)
    {
        return this._http.get(this.URL + this.query).pipe(
            map((res) => res.json));
    }
}
like image 305
david moeller Avatar asked Jun 07 '18 16:06

david moeller


3 Answers

I had a similar problem trying to use a component outside its module. In this case, you have to export a component from your .module.ts:

@NgModule({
  // …
  declarations: [ MyComponent ],
  exports: [ MyComponent ],
  // …
})
like image 125
Viktor Avatar answered Jan 01 '23 13:01

Viktor


check this one

in src/app/app.module.ts

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, FormsModule, HttpClientModule],
  bootstrap: [AppComponent],
})
export class AppModule {}

from this link: https://malcoded.com/posts/why-angular-not-works/

like image 39
Narges Avatar answered Jan 01 '23 14:01

Narges


You need to import your Component and your Service into your app.module.ts and then to declarations and to providers property

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { ImageListComponent } from './image-list.component';
import { ImageService } from './image.service';

@NgModule({
  declarations: [
    AppComponent,
    ImageListComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [ ImageService ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Adjust ImageListComponent path into the import statement.

Teorically when you generate a component with Angular CLI with a command like this:

ng generate component image-list

it should update your app.module.ts file for you.

To generate a service use

ng generate service image
like image 27
firegloves Avatar answered Jan 01 '23 15:01

firegloves