Author avatar

Nikola Brežnjak

Building a Giphy Search Application in Angular 2

Nikola Brežnjak

  • Sep 27, 2018
  • 13 Min read
  • Sep 27, 2018
  • 13 Min read


The focus of this guide is to build an Angular 2 application for searching Giphy's gifs by using their API. For more information about transitioning from Angular 1 to Angular 2 and setting up an Angular application, read my previous guide: Getting Started with an Angular 2 Application.

Adding Content

We want to add something to our app but where to start? One of the first things I do when I start a project is to look at the generated output. Then, I try to find the strings corresponding to that output within the source code. To do this, I use Sublime's Search command. If you're using another editor, it will likely have a similar feature for finding appropriate strings in source code.

If we search for the string app works!, you'll see the string is within the app.component.ts file (in the app folder). This file contains the following:

1import { Component } from "@angular/core";
4  moduleId:,
5  selector: "app-root",
6  templateUrl: "app.component.html",
7  styleUrls: ["app.component.css"]
9export class AppComponent {
10  title = "app works!";

We can see where we would change the app works! text. So, let's change that to Welcome to GiphySearch.


Let's tackle what all this new code means here:

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

The import command defines which parts we'll import and use in our code. In our case, we're importing the Component from the @angular/core module.


If Web development had its own newspaper or magazine, you would probably see headlines like:

  • Directives Obsolete, Components Lend Sight into the Future

  • All Aboard with the Components

  • Components: Why They're Great, Why You Should Use Them, and Why They're Going to Change the World

In short, components are the new directives, which are the building blocks of your angular application.

Here's a sample component:

1import { Component } from "@angular/core";
4  selector: "awesome",
5  template: `
6      <div>
7          Awesome content right here!
8      </div>
9    `
11class Awesome {}

The Component Decorator (@Component) adds additional data to the class that follows it (Awesome), like:

  • selector - tells Angular what element to match
  • template - defines the view

The Component controller is defined by the Awesome class.

We'll be able to use this new tag (awesome) in our HTML like this: <awesome></awesome>, and once rendered to the user in the browser, it will be shown as Awesome content right here!.

In this example, we defined the template using backticks (`). Backticks in the template allow us to define multiline strings.

Even though there are some people who define the template within the @Component, I tend to avoid that and have the template's HTML defined in a separate file. This is also how angular-cli prefers it, so Q.E.D.

Adding Input and Button

Our basic application should have one input field and one button.

To do this, add the following code to the app.component.html file:

1<input name="search">


Having a simple search input field and a button doesn't help much. We want to be able to click the button and we want to output something to the console, to verify it's working correctly.

This is how we define a function that will handle button click in Angular 2:

<button (click)="performSearch()">Search</button>

But now, if you click this button you will get an error. That's because we haven't defined the performSearch function anywhere. In the app.component.ts file, add the following function definition inside the AppComponent class:

1performSearch(): void {
2    console.log('button click is working');

With this, we have defined the performSearch function, which doesn't accept any parameters and it does not return anything (void). Instead, it just prints button click is working to the console. To access this, open up your developer tools to access it.

Taking an Input

What if we would like the string that someone typed in the input field to print to the console? First we need to add a new attribute to the input field:

<input name="title" #searchTerm>

The #searchTerm (a.k.a resolve) tells Angular to bind the input to the new searchTerm variable. Then, we need to pass this variable in the performSearch function like this:

<button (click)="performSearch(searchTerm)">Search</button>

Finally, in the app.components.ts file, change the performSearch function like this:

1performSearch(searchTerm: HTMLInputElement): void {
2    console.log(`User entered: ${searchTerm.value}`);

We added a new parameter to the function (searchTerm), which is of the HTMLInputElement type. And we used the backticks to output the string in the console.log. Notice how we used ${} to output the variable inside the backticks. Because searchTerm is an object, we had to get its value by referencing its value property (${searchTerm.value}).

Giphy Search API

Finally, we come to the cool part: fetching some data from the service and showing it in our app. In our case, we'll show images.

How do we get this API? If you do a simple Google search for giphy api and open the first link you'll get the documentation for their API. We need the search API. If you scroll a bit, you'll find the search link.

Now we can see what kind of a request we need to create to search Giphy's gif database for a certain term. If you open the above link in the browser, you'll see what the service returns. Something like:

Next, we'll retrieve this data from within our app.

Angular HTTP Requests

In Angular 2, we always import the HTTP service from @angular/http module. The following command will do this:

import { HTTP_PROVIDERS } from '@angular/http';

Now we can inject Http into our component.

Let's translate this into our code. In the app.components.ts file, add the following code after the first import:

import { Http, Response } from '@angular/http';

Then, define two variables:

1link = "";
2http: Http;

Then, add the constructor like this:

1constructor(http: Http) {
2    this.http = http;

Finally, add this to the performSearch function:

1var apiLink = + searchTerm.value;
3this.http.request(apiLink).subscribe((res: Response) => {
4  console.log(res.json());

Just for reference, to put it all in one listing, the contents of the app.component.ts file should be:

1import { Component } from "@angular/core";
2import { Http, Response } from "@angular/http";
5  moduleId:,
6  selector: "app-root",
7  templateUrl: "app.component.html",
8  styleUrls: ["app.component.css"]
10export class AppComponent {
11  title = "Welcome to GiphySearch";
12  link = "";
13  http: Http;
15  constructor(http: Http) {
16    this.http = http;
17  }
19  performSearch(searchTerm: HTMLInputElement): void {
20    var apiLink = + searchTerm.value;
22    this.http.request(apiLink).subscribe((res: Response) => {
23      console.log(res.json());
24    });
25  }

If you run this now, you'll get a No provider for Http! error. I made this work by adding the following import line to the main.ts file (in the app folder):

1import { HTTP_PROVIDERS } from "@angular/http";

At the end of the same file, add bootstrap(AppComponent, [HTTP_PROVIDERS]);.

The contents of the app.ts file should be:

1import { bootstrap } from "@angular/platform-browser-dynamic";
2import { enableProdMode } from "@angular/core";
3import { HTTP_PROVIDERS } from "@angular/http";
4import { AppComponent, environment } from "./app/";
6if (environment.production) {
7  enableProdMode();
10bootstrap(AppComponent, [HTTP_PROVIDERS]);

When you run the app, enter a value into the search box, and click the search button, you'll see something like this in your console log:

You can see that we're getting back the result object. In its data property, there are 25 objects which hold information about the images that we want to show in our app. But, how do we show these images in our application?

We see that the API call returns 25 images. Let's save this in some variable for later use:

giphies = [];

Let's store the results from the API call to this variable as well:

this.giphies = res.json().data;

Now, the contents of the app.component.ts file should be:

1import { Component } from "@angular/core";
2import { Http, Response } from "@angular/http";
5  moduleId:,
6  selector: "app-root",
7  templateUrl: "app.component.html",
8  styleUrls: ["app.component.css"]
10export class AppComponent {
11  title = "Welcome to GiphySearch";
12  link = "";
13  http: Http;
14  giphies = [];
16  constructor(http: Http) {
17    this.http = http;
18  }
20  performSearch(searchTerm: HTMLInputElement): void {
21    var apiLink = + searchTerm.value;
23    this.http.request(apiLink).subscribe((res: Response) => {
24      this.giphies = res.json().data;
25      console.log(this.giphies);
26    });
27  }

This is working now, but we don't want to be logging out our objects to the console. We want to show them in our app. To show the image, we need to position ourselves on the object images, then original, and finally on the url property.

We don't want to just show one image but all of the images. We'll use Angular 2's version of the ng-for from Angular 1. It now looks like this:

<img *ngFor="let g of giphies" src="{{g.images.original.url}}">

For reference, here's the full listing of app.component.html:

2  {{title}}
5<input name="search" #searchTerm>
7<button (click)="performSearch(searchTerm)">Search</button>
11<div *ngFor="let g of giphies">
12    <img src="{{g.images.original.url}}">

At this point, if we take a look at the app and search for example for 'funny cats' we'll get this:

Although the result doesn't look sleek, our code does exactly what it's supposed to do. If you want it to look nicer, feel free to add more CSS in the app.components.css file.

Deploying to Github Pages

Angular CLI made things really easy for us, as all we have to do is run the following command:

ng github-pages:deploy

You should get an output similar to this:

1# nikola in ~/DEV/Angular2/GiphySearch on git:master
2→ ng github-pages:deploy
3Built project successfully. Stored in "dist/".
4Deployed! Visit
5Github pages might take a few minutes to show the deployed site.

However, in my case, this just wasn't working. When I opened the link I got the 404. After searching, I found this solution. But again, it didn't resolve the issue.

The following command may work for you. If it doesn't, create your own Github pages by following the official instructions. Then, in your Github pages repository, create a new folder (I called it giphy-search).

In the index.html file change the base url to the name of your folder. In my case that's giphy-search:

<base href="/giphy-search/">

Then, copy the contents of the GiphySearch/dist folder to giphy-search folder.

Add new files, commit them, and push them:

git add .

git commit -m 'adding new project to Github pages'

git push origin master

Now you'll have your app accessible at In my case, the link is

In case you would like to deploy this to your own server, just make sure you execute ng build and then copy the contents of the dist folder to your web server's proper folder. Also, don't forget about the proper base url setting.


In this guide, you learned how to get started with using Angular 2 by building an application for searching Giphy's gifs by using their API. You learned how using Angular-CLI helps remove the pain of setting everything up and some new concepts like Components.

Thank you for reading!