Angular 2 Component With Example - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
Angular 2 Component With Example

Angular 2 Component With Example

Angular 2 Component With Example

Angular 2 is component based. Components combine concepts that we are already familiar with from AngularJS. The Angular 2 Component combines the AngularJS Directive, Controller, and Scope. My article will attempt to make you more comfortable with components by comparing them to what you already know from AngularJS.
In AngularJS, a Component is a special kind of directive that uses a simpler configuration which is suitable for a component-based application structure.
This makes it easier to write an app in a way that's similar to using Web Components or using the new Angular's style of application architecture.
Advantages of Components:
  • simpler configuration than plain directives
  • promote sane defaults and best practices
  • optimized for component-based architecture
  • writing component directives will make it easier to upgrade to Angular
When not to use Components:
  • for directives that need to perform actions in compile and pre-link functions, because they aren't available
  • when you need advanced directive definition options like priority, terminal, multi-element
  • when you want a directive that is triggered by an attribute or CSS class, rather than an element
Components Continued
Angular 2 components make some decisions for you that directives didn’t. For example, Angular 2 components:
  • Always isolate scope (instead of sharing a parent scope)
  • Always restrict ‘E’ (load into custom elements in the DOM)
  • Always bind to a controller (as opposed to $scope)

Creating and configuring a Component

Components can be registered using the .component() method of an AngularJS module (returned by angular.module()). The method takes two arguments:
  • The name of the Component (as string).
  • The Component config object. (Note that, unlike the .directive() method, this method does not take a factory function.)
angular.module('heroApp', []).controller('MainCtrl', function MainCtrl() {
  this.hero = {
    name: 'Spawn'
  };
});
helloDetail.js
angular.module('heroApp').component('heroDetail', {
  templateUrl: 'heroDetail.html',
  bindings: {
    hero: '='
  }
});
index.html
<!-- components match only elements -->
<div ng-controller="MainCtrl as ctrl">
  <b>Hero</b><br>
  <hero-detail hero="ctrl.hero"></hero-detail>
</div>
helloDetail.html
<span>Name: {{$ctrl.hero.name}}</span>
It's also possible to add components via $compileProvider in a module's config phase.

Comparison between Directive definition and Component definition

DirectiveComponent
bindingsNoYes (binds to controller)
bindToControllerYes (default: false)No (use bindings instead)
compile functionYesNo
controllerYesYes (default function() {})
controllerAsYes (default: false)Yes (default: $ctrl)
link functionsYesNo
multiElementYesNo
priorityYesNo
replaceYes (deprecated)No
requireYesYes
restrictYesNo (restricted to elements only)
scopeYes (default: false)No (scope is always isolate)
templateYesYes, injectable
templateNamespaceYesNo
templateUrlYesYes, injectable
terminalYesNo
transcludeYes (default: false)Yes (default: false)
Components have a well-defined lifecycle Each component can implement "lifecycle hooks". These are methods that will be called at certain points in the life of the component. The following hook methods can be implemented:
  • $onInit()  
  • $onChanges(changesObj)  
  • $doCheck()  
  • $onDestroy() 
  • $postLink() 

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.