# Using an API with AJAX
## Lesson Objectives
1. Create a new app
1. Generate a new component
1. Clean up app.component.html
1. Add search component to app
1. Add Form HTML to search component
1. Import Form functionality into the app
1. Set a component property to the value of an input
1. Invoke a function when the user clicks a button
1. Add the ability to make AJAX requests
1. Make an AJAX request
1. Display AJAX results in app
## Create a new app
```
ng new starwars
cd starwars
ng serve --open
```
## Generate a new component
```
ng generate component search
```
## Clean up app.component.html
Set `src/app/app.component.html` contents to:
```html
Star Wars Character Search App
```
## Add search component to app
Edit `src/app/app.component.html`:
```html
Star Wars Character Search App
```
## Add Form HTML to search component
Edit `src/app/search/search.component.html`:
```html
```
## Import Form functionality into the app
Edit `src/app/app.module.ts` to import `FormsModule` and place it as an import:
```javascript
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { SearchComponent } from './search/search.component';
import { FormsModule } from '@angular/forms'; //import FormsModule
@NgModule({
declarations: [
AppComponent,
SearchComponent
],
imports: [
BrowserModule,
FormsModule //add it as an import
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
```
## Set a component property to the value of an input
In `src/app/search/search.component.html`, add `[(ngModel)]` to the text input:
```html
```
Test this by changing the text in the input field
## Invoke a function when the user clicks a button
In `src/app/search/search.component.ts` add a `findCharacter` method to SearchComponent:
```javascript
findCharacter(name){
console.log('finding ' + name);
}
```
Call it in `src/app/search/search.component.html` (also remove the `Search String:{{name}}` test code):
```html
```
Test this by looking in the console
## Add the ability to make AJAX requests
Add `HttpModule` to `src/app/app.module.ts`:
```javascript
import { HttpModule } from '@angular/http'; //import module
@NgModule({
declarations: [
AppComponent,
SearchComponent
],
imports: [
BrowserModule,
HttpModule, //add HttpModule here
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
```
Now edit `src/app/search/search.component.ts` to import appropriate modules:
```javascript
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
```
And tell the constructor of the `SearchComponent` class to add an `http` property:
```javascript
constructor(
private http: Http
) { }
```
## Make an AJAX request
In `src/app/search/search.component.ts`, `findCharacter(name)` should make a request to `swapi.io`:
```javascript
findCharacter(name){
this.http.get('http://swapi.co/api/people/?search=' + name)
.toPromise()
.then(response => console.log(response.json()));
}
```
The `rxjs/add/operator/toPromise` import that we previously wrote adds the ability to change the `Observable` (more on this in another lecture) into a `Promise`
You can test this by looking in the console
## Display AJAX results in app