{"id":2136,"date":"2024-12-22T12:19:18","date_gmt":"2024-12-22T11:19:18","guid":{"rendered":"https:\/\/blog.rabahi.net\/?page_id=2136"},"modified":"2024-12-22T12:49:37","modified_gmt":"2024-12-22T11:49:37","slug":"my-first-service","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=2136","title":{"rendered":"my first service"},"content":{"rendered":"\n<p>Create a service<\/p>\n\n\n\n<p>add public\/data.json with the following content :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n&#x5B;\n    { &quot;lastname&quot;: &quot;Doe&quot;, &quot;firstname&quot;: &quot;John&quot; },\n    { &quot;lastname&quot;: &quot;Doe&quot;, &quot;firstname&quot;: &quot;Jannette&quot; }\n]\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>generate new service to load data.json<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n$ ng generate service my-service\n<\/pre><\/div>\n\n\n<p>update my-service.service.ts file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { Injectable } from '@angular\/core';\n\n\/\/ import http client and rxjs\nimport { HttpClient } from '@angular\/common\/http';\nimport { Observable } from 'rxjs';\n\n@Injectable({\n  providedIn: 'root'\n})\nexport class MyServiceService {\n\n  private dataUrl = '\/data.json';\n\n  constructor(private http: HttpClient) {}\n\n  getData(): Observable&lt;{ lastname: string; firstname: string }&#x5B;]&gt; {\n    return this.http.get&lt;{ lastname: string; firstname: string }&#x5B;]&gt;(this.dataUrl);\n  }\n}\n<\/pre><\/div>\n\n\n<p>update app.module.ts to import HttpClientModule<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { NgModule } from '@angular\/core';\nimport { BrowserModule } from '@angular\/platform-browser';\n\nimport { AppRoutingModule } from '.\/app-routing.module';\nimport { AppComponent } from '.\/app.component';\n\nimport { HttpClientModule } from '@angular\/common\/http';\n\n\/\/ primeNG provider\nimport { provideAnimationsAsync } from '@angular\/platform-browser\/animations\/async';\nimport { providePrimeNG } from 'primeng\/config';\nimport Aura from '@primeng\/themes\/aura';\nimport { MyComponentComponent } from '.\/my-component\/my-component.component';\n\n\/\/ import InputTextModule\nimport { InputTextModule } from 'primeng\/inputtext';\nimport { FormsModule } from '@angular\/forms';\n\n@NgModule({\n  declarations: &#x5B;\n    AppComponent,\n    MyComponentComponent\n  ],\n  imports: &#x5B;\n    BrowserModule,\n    AppRoutingModule,\n    InputTextModule,\n    FormsModule,\n    HttpClientModule\n  ],\n  providers: &#x5B;\n    \/\/ add primeNG providers\n    provideAnimationsAsync(),\n        providePrimeNG({\n            theme: {\n                preset: Aura\n            }\n        })  \n  ],\n  bootstrap: &#x5B;AppComponent]\n})\nexport class AppModule { }\n<\/pre><\/div>\n\n\n<p>update my-component.component.ts<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { Component } from '@angular\/core';\n\n\/\/ import service\nimport { MyServiceService } from '..\/my-service.service';\n\n@Component({\n  selector: 'app-my-component',\n  standalone: false,\n  templateUrl: '.\/my-component.component.html',\n  styleUrl: '.\/my-component.component.css'\n})\nexport class MyComponentComponent {\n\n  firstname: string = 'John';\n\n  \/\/ init data array\n  data: { lastname: string; firstname: string }&#x5B;] = &#x5B;];\n\n  \/\/ inject service\n  constructor(private myService: MyServiceService) { }\n\n  \/\/ load data when component init\n  ngOnInit(): void {\n    this.myService.getData().subscribe((data) =&gt; {\n      this.data = data;\n    });\n  }\n\n}\n<\/pre><\/div>\n\n\n<p>finally my-component.component.html<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;p&gt;my-component works!&lt;\/p&gt;\n\n&lt;div class=&quot;card flex justify-center&quot;&gt;\n    &lt;input type=&quot;text&quot; pInputText &#x5B;(ngModel)]=&quot;firstname&quot; \/&gt;\n&lt;\/div&gt;\n\nHi {{ firstname }} !\n\n&lt;!-- add data --&gt;\n&lt;h1&gt;Data&lt;\/h1&gt;\n&lt;ul&gt;\n  &lt;li *ngFor=&quot;let elt of data&quot;&gt;\n    {{ elt.lastname }} {{ elt.firstname }}\n  &lt;\/li&gt;\n&lt;\/ul&gt;\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Create a service add public\/data.json with the following content : generate new service to load data.json update my-service.service.ts file update app.module.ts to import HttpClientModule update my-component.component.ts finally my-component.component.html<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2069,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2136","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/2136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2136"}],"version-history":[{"count":7,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/2136\/revisions"}],"predecessor-version":[{"id":2143,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/2136\/revisions\/2143"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/2069"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}