Angular2 series – Component, Directive, Pipe and Service
2017-01-24 23:48
281 查看
原文链接:https://julienrenaux.fr/2015/12/13/angular2-series-component-directive-pipe-and-service/
Learn how to create Components, Directives, Pipes and Services with Angular2
Table of Contents [hide]
Component
Demo
Lifecycle hooks
Directive
Result
Pipe
Demo
Built in pipes
Service
Demo
Application
Bootstrap
This post is part of a series of posts about Angular2. You can find them all in the introduction:
http://julienrenaux.fr/2015/11/30/angular2-series-introduction/
Before we start and to make sure we are in line on the terminology, I will use the name AngularJS to define AngularJS 1.x and Angular2 to define AngularJS 2.x.
Official docs
Let’s create a component that lists the US Democratic Party presidential candidates.
When a component is instantiated, Angular2 creates a shadow DOM for the component (Shadow DOM provides encapsulation for the JavaScript, CSS, and templating in a Web Component). Then, the template and styles are injected inside it.
Learn a bit more about Web component and specifically about the
Angular2 series – Template Syntax.
You can now use your component by inserting it into your html page:
http://embed.plnkr.co/cUCWoUDRzd31YRRbo5Rg/preview
Official docs
Official docs
Let’s get back to our previous component and this time, let’s make our presidential candidates red. To do so, we are going to create the
Notice that in order to obtain a reference to our Presidential Candidate element we injected
Official docs for ElementRef
To modify the element style we injected
Official docs for Renderer
Then we can add the
http://embed.plnkr.co/iJiZVqixM0qAo4RMelB7/preview
A better explanation of what is a pipe is available in the previous article of this series of posts:
Angular2 series – Template Syntax
Official docs
Let’s get back again to our previous component and this time, let’s create a pipe to transform our presidential candidates last name to uppercase.
First we create
Then let’s add this pipe to our
http://embed.plnkr.co/L1ERY1Pn6qmGl0B1hi0K/preview
Official docs
Now let’s consume this service on our
We have decoupled the presidential candidates retrieval with the component that displays them. It is now easier for other components to consume this data.
http://embed.plnkr.co/z1B96b4OX7BHwkT0492G/preview
Our application is now ready to be rendered. Insert your app component in your
Thanks for reading, you can interact about this post by leaving a comment here, or directly on
Twitter and
Facebook!
Angular2 series – Component, Directive, Pipe and Service
PreviousNextLearn how to create Components, Directives, Pipes and Services with Angular2
Table of Contents [hide]
Component
Demo
Lifecycle hooks
Directive
Result
Pipe
Demo
Built in pipes
Service
Demo
Application
Bootstrap
This post is part of a series of posts about Angular2. You can find them all in the introduction:
http://julienrenaux.fr/2015/11/30/angular2-series-introduction/
Before we start and to make sure we are in line on the terminology, I will use the name AngularJS to define AngularJS 1.x and Angular2 to define AngularJS 2.x.
Component
A component is what you used to call a directive in AngularJS. It contains a template, styles, a list of injectables (directives, services) and a selector.Official docs
Let’s create a component that lists the US Democratic Party presidential candidates.
Learn a bit more about Web component and specifically about the
<template>tag by reading the previous article of this series of posts:
Angular2 series – Template Syntax.
You can now use your component by inserting it into your html page:
Demo
http://embed.plnkr.co/cUCWoUDRzd31YRRbo5Rg/preview
Lifecycle hooks
In the previous example, we used thengOnInitClass method to dump a message
[Component] navbar ngOnInitin the console. It is called only when the component is initiated. It exists several hooks that make your life easier when it comes to plug yourself in between component life phases.
ngOnChanges(if any bindings have changed)
ngOnInit(after the first check only)
ngOnDestroy(at the very end before destruction) Implement this interface to get notified when any data-bound property of your directive changes
ngDoCheck
ngAfterContentInit
ngAfterContentChecked
ngAfterViewInit
ngAfterViewChecked
Official docs
Directive
Directives allow you to attach behaviour to elements in the DOM. It is also what you used to call a directive in AngularJS, but without a proper view. You can therefore place as many directives as you want on one DOM-element. This is not possible with components.Official docs
Let’s get back to our previous component and this time, let’s make our presidential candidates red. To do so, we are going to create the
redifydirective:
_element: ElementRef.
Official docs for ElementRef
To modify the element style we injected
renderer: Renderer, which is a service that gives you methods to manipulate the style of a particular element.
Official docs for Renderer
Then we can add the
redifydirective to our component:
Result
http://embed.plnkr.co/iJiZVqixM0qAo4RMelB7/preview
Pipe
A pipe in Angular2 is the equivalent of filters in AngularJS. As in AngularJS, pipes can be stateless (pure functions, not reevaluated) or stateful (has dependencies that can modify the output).A better explanation of what is a pipe is available in the previous article of this series of posts:
Angular2 series – Template Syntax
Official docs
Let’s get back again to our previous component and this time, let’s create a pipe to transform our presidential candidates last name to uppercase.
First we create
lastnameUppercasepipe:
navbarcomponent in order to consume it.
Demo
http://embed.plnkr.co/L1ERY1Pn6qmGl0B1hi0K/preview
Built in pipes
In Angular2 you have access to the following pipes for free:currency
date
uppercase
json
limitTo
lowercase
async
decimal
percent
Service
Now that we saw how to create a component, a directive and a pipe, we are going to clean up our code and separate the data retrieval (the presidential candidates) into a service.Official docs
navbarcomponent:
Demo
http://embed.plnkr.co/z1B96b4OX7BHwkT0492G/preview
Application
AngularJS
With AngularJS an application was a simple module. It had no difference from any other module of your application.Angular2
With Angular2, it is similar. An application is a simple component as any other component of your application. It is just the root component that basically contains the scaffolding of your page.Header,
Navbar,
Contentand
Footerare custom components, they do not exist within Angular2 core.
Bootstrap
Now that you know how to create components and a root component (or app), you need to bootstrap the application.AngularJS
In AngularJS you could useangular.bootstrap(document, ['yourApp']);or the
ng-appdirective
<body ng-app="yourApp">.
Angular2
In Angular2 it is very similar.index.htmlfile and reload the browser, your app is ready!
Twitter and
Facebook!
相关文章推荐
- Difference between component and directive in Angular 2
- angularjs directive and component 指令与组件 ( 1.5.0 以后 )
- [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects
- [Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2
- angularjs controller, service, directive 的demo
- angularJs-The factory and service of angularJs
- Angular1.x 之Providers (Value, Factory, Service and Constant )
- 我也谈“the difference between Factory, Service, and Provider in Angular”
- AngularJS + ui-router + RequireJS异步加载注册controller/directive/filter/service
- 理解 AngularJS $q service and promises
- How to use Spring @Component, @Repository, @Service and @Controller Annotations?
- [Angular 2] Share a Service Across Angular 2 Components and Modules
- Spring @Component, @Repository, @Service and @Controller Annotations
- [Angular 2] Generate and Render Angular 2 Template Elements in a Component
- Angular2初级篇之component和service
- [Angular] AuthService and AngularFire integration
- [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword
- Angular 根据 service 的状态更新 directive
- [Angular 2] Directive intro and exportAs
- AngularJS Service vs Factory - Once and for all