diff --git a/cms/articles/1480177579/article-1480177579.component.js b/cms/articles/1480177579/article-1480177579.component.js new file mode 100644 index 0000000..3db15ac --- /dev/null +++ b/cms/articles/1480177579/article-1480177579.component.js @@ -0,0 +1,144 @@ +import { Component } from '@angular/core'; +import { DomSanitizer } from '@angular/platform-browser'; +import highlight from 'highlight.js'; + +import { xblogTableContentService } from 'xblog-cores/modules'; +import { resourceUtils } from 'xblog-cores/utils'; + + +export var article1480177579Component = Component({ + selector: 'article', + templateUrl: './templates/article-1480177579.html', + host: { + '[class.xblog-article-1480177579]': 'true' + } +}) + .Class({ + constructor: [ + DomSanitizer, + xblogTableContentService, + + function (sanitizer, tableContentService) { + this.id = 1480177579; + this.sanitizer = sanitizer; + this.tableContentService = tableContentService; + } + ], + + ngOnInit: function () { + this.tableContents = this.tableContentService + .getBuilder() + .addHeadings([ + { id: 'simple-service', name: 'Simple Service' }, + { id: 'inject-service-to-component', name: 'Inject Service to Component' }, + { id: 'register-services-at-the-module-level', name: 'Register Services at the Module Level' }, + ]) + .build(); + + this.examples = { + simpleService: { + sourceCode: { + name: 'Simple Service', + link: resourceUtils.getGithubArticleFileLink(this.id, 'ng-on-init/child.component.js') + }, + codeBlocks: this.getCodeBlock(simeplService) + }, + injectServiceToComponent: { + sourceCode: { + name: 'Inject Service to Component', + link: resourceUtils.getGithubArticleFileLink(this.id, 'ng-on-init/child.component.js') + }, + codeBlocks: this.getCodeBlock(injectServiceToComponent) + }, + registerServicesAtTheModuleLevel: { + sourceCode: { + name: 'Register Services at the Module Level', + link: resourceUtils.getGithubArticleFileLink(this.id, 'ng-on-init/child.component.js') + }, + codeBlocks: this.getCodeBlock(registerServicesAtTheModuleLevel) + } + } + }, + + getCodeBlock: function (getter, lang) { + var _langs = lang ? [lang] : ['javascript', 'html', 'css']; + + var _codeBlock = highlight.highlightAuto(getter().replace('\n', '').replace(/^ /gm, ''), _langs).value; + + return this.sanitizer.bypassSecurityTrustHtml(_codeBlock); + } + }); + +function simeplService() { + return ` + import { Class } from '@angular/core'; + + export const HeroService = Class({ + constructor: function() { + this.heroes = [ + { + id: 1, + name: 'X-Men' + }, + { + id: 2, + name: 'Spider Man' + }, + ... + ]; + } + + getHeroes: function() { + return this.heroes; + } + });`; +} + +function injectServiceToComponent() { + return ` + import { HeroService } from './hero.service'; + + export const HeroComponent = Component({ + .... + providers: [ HeroService ] + }).Class({ + constructor: [HeroService, function(heroService){ + this.heroService = heroService; + }] + + getHeroes: function() { + this.heroes = this.heroService.getHeroes(); + } + });`; +} + +function registerServicesAtTheModuleLevel() { + return ` + /* AppModule */ + import { NgModule } from '@angular/core'; + import { HeroService } from './hero.service'; + + export const AppModule = NgModule({ + ... + providers: [ HeroService ] // We can put our services here! + }).Class({ + constructor: function() {} + }); + + /* HeroComponent */ + import { HeroService } from './hero.service'; + + export const HeroComponent = Component({ + .... + // providers: [ HeroService ] + // we don't need register at providers because we registered at module level + }).Class({ + constructor: [HeroService, function(heroService){ + this.heroService = heroService; + }] + + getHeroes: function() { + this.heroes = this.heroService.getHeroes(); + } + });`; +} \ No newline at end of file diff --git a/cms/articles/1480177579/index.js b/cms/articles/1480177579/index.js new file mode 100644 index 0000000..4eacf65 --- /dev/null +++ b/cms/articles/1480177579/index.js @@ -0,0 +1,15 @@ +import { resourceUtils } from 'xblog-cores/utils'; +import { article1480177579Component } from './article-1480177579.component'; + +export var article1480177579 = { + id: 1480177579, + title: 'Services', + postedDate: 'Sat Nov 26 2016 23:26:19 GMT+0700 (ICT)', + author: 'Thanh Tran', + cover: resourceUtils.getImg('xblog-home-cover.jpg'), + routeLink: resourceUtils.getArticleRouteLink('services-1480177579.html'), + relatedArticles: [], + tags: [], + description: 'If a piece of code is needed by many components in our application. A good practice is to create a single reusable service.', + content: article1480177579Component, +}; diff --git a/cms/articles/1480177579/templates/article-1480177579.html b/cms/articles/1480177579/templates/article-1480177579.html new file mode 100644 index 0000000..5c65ef7 --- /dev/null +++ b/cms/articles/1480177579/templates/article-1480177579.html @@ -0,0 +1,58 @@ +
Every application is composed of a lot of subsystems that do different things like logging, data access, caching, specific application domain knowledge, etc. Depending on the type of application that we are building or the framework that we are using there’s different ways to represent these subsystems.
+Angular 2 uses the concept of services, an Angular 2 service is a class that encapsulates some sort of functionality and provides it as a service for the rest of our application.
+ +If we have an Angular 1 background we’ll welcome this simplification in the Angular conceptual model: there’s no more service/factory/constant/provider conundrum. In Angular 2 there’s only services.
+ +Using Angular 2 DI to Inject Our Hero Service to the Hero List Component. We inject it into the HeroListComponent via its constructor:
+ +When would we want to do that? – we may wonder. Well, whenever we want to use the same instance of a service for our whole application. When we register services at the module level, we can use service in component without register at providers.
+ +This is an example of the registration service at the module level and use the service in components.
+ +