Fireship · @fireship

Autocomplete Search with Angular4 and Firebase

Uploaded June 22, 2017 · Length 4:36

Autocomplete Search with Angular4 and Firebase

Summary

build autocomplete search with angular4 and firebase, real-time database, and rxjs

  • angular4
  • firebase
  • autocomplete
  • real-time database
  • rxjs

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    in this episode we're going to build an

  2. 0:02

    autocomplete feature using angular for

  3. 0:04

    and firebase users will be able to type

  4. 0:07

    into a search field and the results will

  5. 0:09

    automatically be updated in the fields

  6. 0:11

    below in order to demonstrate this

  7. 0:15

    feature we have the real-time database

  8. 0:17

    populated with a series of movies the

  9. 0:20

    search function will match string

  10. 0:22

    patterns to the title on a database

  11. 0:24

    similar to the sequel like operator

  12. 0:27

    let's get the app started by building

  13. 0:30

    the service the service is very simple

  14. 0:36

    all we needed to do is return a firebase

  15. 0:39

    list observable that we can ask a query

  16. 0:42

    to and this query can take either just a

  17. 0:46

    regular string object or it can take a

  18. 0:48

    subject from rxjs passing to the subject

  19. 0:56

    allows you to call next on the underline

  20. 0:58

    subject which is a lot easier more

  21. 1:00

    intuitive way to filter that query now

  22. 1:06

    we can jump onto the component the first

  23. 1:10

    thing we do is build an input field

  24. 1:12

    where the user can enter their search

  25. 1:14

    value

  26. 1:16

    then we'll attach a function to the

  27. 1:18

    keydown event which will run every time

  28. 1:21

    the user presses the cubes field from

  29. 1:24

    there we will populate an array of

  30. 1:26

    movies and then loop through them using

  31. 1:28

    the ng 4 directive and as the final

  32. 1:32

    touch will add the ng-if directive to a

  33. 1:34

    div that will only be displayed look the

  34. 1:36

    movies array is empty in the component

  35. 1:40

    type script we start by injecting the

  36. 1:42

    service that we built earlier and we

  37. 1:44

    also import the rxjs subject class now

  38. 1:50

    we declare a couple variables the movies

  39. 1:52

    variable is going to hold the firebase

  40. 1:54

    list observable that start at and end at

  41. 1:57

    variables both get a hold subject and