DEV Community

ismail-dinar
ismail-dinar

Posted on

3 2

Angular in a Nutshell #Part2: Components

In Angular, a component is a class marked by @Component decorator that defines a view and the logic behind it.

@Component({
  selector: ...,
  template: ...,
  styles: ...,
})
Enter fullscreen mode Exit fullscreen mode

The object provided to @Component decorator represents the component metadata.

  • Selector: Unique identifier that defines how the current component is represented in the HTML DOM.
selector: 'my-app'
Enter fullscreen mode Exit fullscreen mode
  • Template: Defines the template of the component as an HTML string.
template: '<h1>My Angular Component</h1>'
Enter fullscreen mode Exit fullscreen mode

When you have a complex template it's recommended to separate it in an HTML file and use the templateUrl instead

templateUrl: './app.component.html'
Enter fullscreen mode Exit fullscreen mode
  • Styles: Defines the styles for a component as an array of strings.
styles:['h1 { color: red; }']
Enter fullscreen mode Exit fullscreen mode

The same thing as for template applies here, if you have a complex style, it's better to declare it in a separate file and then provide it to the component through the styleUrls property.

styleUrls: ['./app.component.css']
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
jacob_charheci profile image
Jacob Charheci

Thank you for sharing..

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More