D
D
danilr2019-03-31 09:43:27
Angular
danilr, 2019-03-31 09:43:27

Why is data not being passed from an Angular component?

Friends, I ask for help in correcting and correcting MY code, the links do not help me, I am already tormented and confused about doing the examples from the links, since everything is different in the example, but it doesn’t work out exactly for you. I would be extremely grateful for your help!
The task - I enter through an input in one component, the line should go through the service to another component and a request to the API is called as a parameter with this new line. I have something going wrong - it seems the line does not reach.
The component where I enter the input:

import { Component, OnInit, Input, EventEmitter} from '@angular/core';
import { HttpService } from '../http.service'

@Component({
  selector: 'search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss']
})

export class SearchComponent{
  InputFilm: string;

  constructor(private httpService: HttpService){
    this.httpService.InputFilm.subscribe(film => this.InputFilm = film)
  }

  handlerEnterSearch(){
    this.httpService.setInputFilm(this.InputFilm);
    this.httpService.getFilms().subscribe(data =>{
      this.httpService.setFilmsData(data)
      console.log('data: ', data);
    });
  }
  ngOnInit(){}
}

Component where I get:
import { Component, OnInit } from '@angular/core';
import { HttpService } from '../http.service'

@Component({
  selector: 'result',
  templateUrl: './result.component.html',
  styleUrls: ['./result.component.scss']
})
export class ResultComponent implements OnInit {

  films:any;

  constructor(private httpService: HttpService){
    this.films = this.httpService.films
  }
  ngOnInit() {}
}

And service:
import {Injectable} from '@angular/core';
import {EventEmitter} from '@angular/core';
import {HttpClient,HttpParams} from '@angular/common/http';
import {Observable} from 'rxjs';
import { map } from 'rxjs/operators';
  
@Injectable()
export class HttpService{
    baseURL: string = "https://api.themoviedb.org/3";
  searchURL: string = "/search/movie"

  currentInputFilm: string;
  films: any

  InputFilm:EventEmitter<any> = new EventEmitter();

    constructor(private http: HttpClient){ }

    setInputFilm(inputFilm:string){
        this.currentInputFilm = inputFilm
        console.log('this.inputFilm: ', this.currentInputFilm);

    }
    setFilmsData(data){
        this.films = data
    }
      
    getFilms(){

        const params = new HttpParams().set('api_key', 'd6a5ac2d3d7cab11a084ca8080421b20').set('query', this.currentInputFilm)
        const options = {params: params};
        return this.http.get(this.baseURL+this.searchURL, options)
    }
}

Please, who is not difficult to spend a couple of minutes to help, write what and where to fix.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question