---
agentTools:
  projectIndex: https://laravel-angular.readme.io/llms.txt
---

# Angular Component

Now we need to create our first component which will contain the form that the user needs to fill in order to add a new **Post**.

```
php artisan ng:component create_post_form
```

[block:callout]
{
  "type": "success",
  "title": "Components encourage code reuse",
  "body": "Aim to create a component for every feature so that you can reuse it later on."
}
[/block]

Open `angular/app/components/create_post_form/create_post_form.component.html` in your editor and add the relevant HTML form.

[block:code]
{
  "codes": [
    {
      "code": "<form ng-submit=\"vm.submit()\">\n  \n  \t<md-input-container>\n      <label>Name</label>\n  \t\t<input type=\"text\" ng-model=\"vm.name\">\n    </md-input-container>\n  \n  \t<md-input-container>\n      <label>Topic</label>\n    \t<input type=\"text\" ng-model=\"vm.topic\">\n     </md-input-container>\n  \n    <md-button type=\"submit\" class=\"md-primary md-raised\">Create post</md-button>\n  \n</form>",
      "language": "html",
      "name": "create_post_form.component.html"
    }
  ]
}
[/block]

And then update your `create_post_form.component.js` code to write the controller's logic

[block:code]
{
  "codes": [
    {
      "code": "class CreatePostFormController{\n    constructor(API, ToastService){\n        'ngInject';\n      \n \t     \tthis.API = API;\n  \t    this.ToastService = ToastService;\n    }\n  \n   submit(){\n      var data = {\n        name: this.name,\n        topic: this.topic,\n      };\n      \n     \t this.API.all('posts').post(data).then((response) => {\n         this.ToastService.show('Post added successfully');\n       });\n    }\n}\n\nexport const CreatePostFormComponent = {\n    templateUrl: './views/app/components/create_post_form/create_post_form.component.html',\n    controller: CreatePostFormController,\n    controllerAs: 'vm',\n    bindings: {}\n}",
      "language": "javascript",
      "name": "create_post_form.component.js"
    }
  ]
}
[/block]

Then you need to edit your angular/index.components.js to import this component

[block:code]
{
  "codes": [
    {
      "code": "import {CreatePostFormComponent} from './app/components/create_post_form/create_post_form.component';\n\nangular.module('app.components')\n  .component('createPostForm', CreatePostFormComponent);\n",
      "language": "javascript"
    }
  ]
}
[/block]

[block:callout]
{
  "type": "success",
  "title": "EcmaScript 6",
  "body": "We are writing in EcmaScript 6 which is the upcoming version of JavaScript (EcmaScript 5). Here's a good resource for [learning EcmaScript 6](https://babeljs.io/docs/learn-es2015/)."
}
[/block]

> Prefer a screencast?

[block:html]
{
  "html": "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/_ZWV9KBK2N8\" frameborder=\"0\" allowfullscreen></iframe>"
}
[/block]