<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Joshua Daza Rincon</title>
    <description>The latest articles on Forem by Joshua Daza Rincon (@josdazari).</description>
    <link>https://forem.com/josdazari</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F270470%2F39ef0856-5bd5-46be-be1f-0ddc2dbc58e7.jpg</url>
      <title>Forem: Joshua Daza Rincon</title>
      <link>https://forem.com/josdazari</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/josdazari"/>
    <language>en</language>
    <item>
      <title>Simple Angular Service using signals</title>
      <dc:creator>Joshua Daza Rincon</dc:creator>
      <pubDate>Wed, 15 Nov 2023 04:30:34 +0000</pubDate>
      <link>https://forem.com/josdazari/simple-angular-service-using-signals-5hli</link>
      <guid>https://forem.com/josdazari/simple-angular-service-using-signals-5hli</guid>
      <description>&lt;p&gt;Related to Angular 17th launching I try to learn how works signals. So check how to share data between 2 components.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://stackblitz.com/edit/stackblitz-starters-zputtr?file=src%2Fcomponents%2Fheader%2Fheader.component.html" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Firstable this example use standAlone components but the workflow is very similar compared with previous versions.&lt;/p&gt;

&lt;p&gt;Create a service with data attribute (assigning the signal) with a number value. and set 2 methods to manage signal value. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;store.ts&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable, signal } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class Store {

  private data = signal(0);

  setDataSignal(val: number) {
    this.data.set(val);
  }

  get DataSignal() {
    return this.data();
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Consuming signal between components...
&lt;/h2&gt;

&lt;p&gt;Now in main (standAlone component) using a constructor make an instance from Store to  local store attribute. Then create a &lt;strong&gt;get&lt;/strong&gt; returning the signal value executed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;main.ts&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'zone.js';
import { Component, OnInit } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { Store } from '../store/store';
import { HeaderComponent } from './components/header/header.component';

@Component({
  selector: 'app-root',
  standalone: true,
  templateUrl: './main.html',
  imports: [HeaderComponent],
})
export class App {

  constructor(private store: Store) {}

  get dataValue() {
    return this.store.dataSignal;
  }
}

bootstrapApplication(App);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In signals the expression require an execution to listen global state. It can be from service or component, for this example &lt;strong&gt;from service&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I prefer avoid to write html code in &lt;strong&gt;main.ts&lt;/strong&gt; component to keep all separated and more readable but if you prefer just replace &lt;code&gt;templateUrl: './main.html'&lt;/code&gt; with &lt;code&gt;template: '&amp;lt;h2&amp;gt;This is main with Data {{dataValue}}&amp;lt;/h2&amp;gt;'&lt;/code&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;main.html&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;section&amp;gt;
  &amp;lt;h2&amp;gt;This is main with Data {{dataValue}}&amp;lt;/h2&amp;gt;
&amp;lt;/section&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Second Component
&lt;/h3&gt;

&lt;p&gt;Create a header component on this path &lt;strong&gt;components/header&lt;/strong&gt; as a traditional angular component.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;header.ts&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
import { Store } from '../../../store/store';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css'],
  standalone: true,
})
export class HeaderComponent {
  constructor(private store: Store) {}

  get dataValue() {
    return this.store.Data;
  }

  changeData(e: any) {
    this.store.setData(e);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to get dataValue is the same approach but to change data create changeData method to pass the new value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;header.html&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;header&amp;gt;
  Header Component with DataValue: &amp;lt;strong&amp;gt;{{ dataValue }}&amp;lt;/strong&amp;gt; &amp;lt;br /&amp;gt;
  &amp;lt;button (click)="changeData(dataValue + 2)"&amp;gt;change data&amp;lt;/button&amp;gt;
&amp;lt;/header&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using (click) directive set new value adding a number to the current dataValue. That trigger a new value listened globally for all components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That new way to share data is more simple to write and read, signals definitely does not replace the previous way since&lt;br&gt;
RxJS has a huge range of operators and features but is a huge beginning...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/joshuadazar/Services-with-signals"&gt;Github code&lt;/a&gt; &lt;/p&gt;

</description>
      <category>signals</category>
      <category>angular</category>
      <category>services</category>
      <category>joshuadaza</category>
    </item>
  </channel>
</rss>
