<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2964959029992147971</id><updated>2026-04-14T21:32:11.687+05:30</updated><category term="Core java Interview Questions"/><category term="java programming interview questions"/><category term="Java SE"/><category term="Java Programs"/><category term="c programming examples for beginners"/><category term="C language interview programs"/><category term="Python Programming"/><category term="Angular"/><category term="java Collections interview Questions"/><category term="Concept and Interview Questions"/><category term="java interview programs"/><category term="Collections"/><category term="concept and program"/><category term="Java EE"/><category term="Java 8"/><category term="c programming language"/><category term="exception handling interview questions"/><category term="hibernate interview questions"/><category term="ArrayList"/><category term="Basic Java Example programs"/><category term="Hashtable"/><category term="Javascript interview questions"/><category term="javascript"/><category term="java string interview programs"/><category term="Class-Program"/><category term="Find Output of Program"/><category term="arrays"/><category term="Hibernate Tutorial"/><category term="core java multiple choice questions with answers"/><category term="java programs for freshers"/><category term="treeset"/><category term="Python"/><category term="Servlets Tutorial"/><category term="hashset"/><category term="java sorting algorithms"/><category term="AI tools"/><category term="Design Pattern"/><category term="Jsp"/><category term="LinkedHashSet"/><category term="hashmap"/><category term="hibernate"/><category term="java8"/><category term="linux unix commands"/><category term="technology"/><category term="Big data Hadoop  interview questions and answers for freshers and experienced"/><category term="JDBC"/><category term="Jsp interview questions"/><category term="Trending"/><category term="apache spark examples java"/><category term="java interview questions"/><category term="Files"/><category term="HCQL"/><category term="HQL"/><category term="HTML interview questions"/><category term="Hibernate named query"/><category term="ListIterator"/><category term="Multithreading interview questions"/><category term="Oracle"/><category term="Quantitative Aptitude questions in java for freshers"/><category term="Sorting algorithms"/><category term="angular interview questions"/><category term="finally with return"/><category term="java"/><category term="java design pattern interview questions and answers"/><category term="java eclipse"/><category term="java load properties file example"/><category term="java test"/><category term="jobs"/><category term="spring"/><category term="springboot"/><category term="technews"/><category term="try except example"/><title type='text'> InstanceOfJava</title><subtitle type='html'>Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>saidesh</name><uri>http://www.blogger.com/profile/03671387577197113199</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>593</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-1310830413272703149</id><published>2026-02-28T15:23:00.013+05:30</published><updated>2026-02-28T15:32:29.340+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="angular interview questions"/><title type='text'>Angular Interview Questions &amp; Answers for 10 Years Experience (2026)</title><content type='html'>&lt;p style=&quot;text-align: left;&quot;&gt;If you have 10 years of Angular experience, interviewers won&#39;t ask you what a component is. They&#39;ll dig into architecture decisions, performance trade-offs, RxJS internals, state management patterns, and how you lead teams&amp;nbsp; because that&#39;s what a principal or staff-level Angular engineer is expected to own.&lt;/p&gt;

&lt;p style=&quot;text-align: left;&quot;&gt;This guide covers the exact questions you&#39;ll face in a senior Angular interview, with the depth of answers that actually impress experienced interviewers.&lt;/p&gt;

&lt;p style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3&gt;What Interviewers Really Expect at 10 Years&lt;/h3&gt;

&lt;p&gt;A decade of experience means you should go beyond knowing the API&amp;nbsp; you should be able to &lt;strong&gt;justify why&lt;/strong&gt;. Expect questions across three dimensions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Technical depth&lt;/strong&gt;&amp;nbsp; deep framework internals, RxJS, Signals, DI, compilation&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Architecture &amp;amp; system design&lt;/strong&gt;&amp;nbsp; scalable app structure, micro-frontends, SSR&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Leadership &amp;amp; soft skills&lt;/strong&gt;&amp;nbsp; mentoring, code reviews, owning tech debt, pushing back on product decisions&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Interviewers often set the tone early: &lt;em&gt;&lt;b&gt;Since you&#39;ve said you know Angular really well, can I ask hard questions and expect strong answers?&lt;/b&gt;&lt;/em&gt;&amp;nbsp;they&#39;re testing self-awareness as much as technical knowledge.&lt;/p&gt;

&lt;h3&gt;1. Change Detection &amp;amp; Zone.js Internals&lt;/h3&gt;

&lt;h4&gt;Q: How does Angular&#39;s change detection work under the hood? When would you bypass Zone.js entirely?&lt;/h4&gt;

&lt;p&gt;Angular&#39;s change detection tracks when the UI needs to re-render. By default, it uses &lt;strong&gt;Zone.js&lt;/strong&gt;, which monkey-patches all async browser APIs&amp;nbsp;&amp;nbsp;setTimeout, Promise, addEventListener, XHR&amp;nbsp; and notifies Angular to walk the entire component tree from root to leaf checking for changes.&lt;/p&gt;

&lt;p&gt;At 10 years, you should know:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The component tree is checked &lt;strong&gt;top-down&lt;/strong&gt; in a single pass&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;OnPush&lt;/strong&gt; strategy only re-checks when input references change or an async observable emits&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;ChangeDetectorRef.detach()&lt;/code&gt; removes a component from the tree entirely&amp;nbsp; useful for real-time data components&lt;/li&gt;
  &lt;li&gt;Angular 17+ &lt;strong&gt;Signals&lt;/strong&gt; provide a zone-free reactive model where only the specific signal consumer re-renders&lt;/li&gt;
&lt;/ul&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;constructor(private ngZone: NgZone) {}

ngOnInit() {
  this.ngZone.runOutsideAngular(() =&amp;gt; {
    this.map.on(&#39;mousemove&#39;, (e) =&amp;gt; {
      // runs hundreds of times per second — no CD triggered
      this.updateCoords(e.latlng);
    });
  });
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Follow-up interviewers ask:&lt;/strong&gt; &lt;em&gt;&quot;How would you debug excessive change detection cycles?&quot;&lt;/em&gt;&amp;nbsp; Use Angular DevTools profiler, identify which component subtrees are re-rendering unnecessarily, apply OnPush + trackBy + async pipe.&lt;/p&gt;

&lt;h4&gt;Q: What is the difference between JIT and AOT compilation? Why does AOT matter in production?&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;JIT (Just-in-Time):&lt;/strong&gt; Templates are compiled in the browser at runtime&amp;nbsp; slower startup, larger bundle, but faster dev iteration. &lt;strong&gt;AOT (Ahead-of-Time):&lt;/strong&gt; Templates compile at build time&amp;nbsp; the browser receives pre-compiled, optimized JS with faster startup, smaller bundle, and template errors caught at build time rather than runtime.&lt;/p&gt;

&lt;p&gt;At 10 years, you add:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;AOT enables &lt;strong&gt;tree-shaking&lt;/strong&gt; of unused Angular framework code&lt;/li&gt;
  &lt;li&gt;Angular Ivy (default since v9) made AOT the default for both dev and prod&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Incremental DOM&lt;/strong&gt; under Ivy reduces memory allocation compared to the old View Engine&#39;s virtual DOM approach&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;2. RxJS &amp;amp; Reactive Programming&lt;/h3&gt;

&lt;h4&gt;Q: When would you use switchMap vs concatMap vs mergeMap vs exhaustMap?&lt;/h4&gt;

&lt;p&gt;This is a classic senior filter question. Each handles inner observable subscription differently:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Operator&lt;/th&gt;
      &lt;th&gt;Behavior&lt;/th&gt;
      &lt;th&gt;Best Use Case&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;switchMap&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Cancels previous inner observable on new emission&lt;/td&gt;
      &lt;td&gt;Typeahead search, route params&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;concatMap&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Queues&amp;nbsp; waits for previous to complete&lt;/td&gt;
      &lt;td&gt;Sequential API calls, ordered operations&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;mergeMap&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;All run in parallel&lt;/td&gt;
      &lt;td&gt;Independent parallel requests&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;exhaustMap&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Ignores new emissions while inner is active&lt;/td&gt;
      &lt;td&gt;Login button, prevent duplicate submits&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A 10-year engineer adds: &lt;em&gt;&quot;I&#39;ve used exhaustMap on form submits to prevent double POST requests — it&#39;s a one-liner fix for a bug junior devs spend days debugging.&quot;&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;Q: How do you prevent memory leaks from RxJS subscriptions in Angular?&lt;/h4&gt;

&lt;p&gt;There are multiple valid approaches — interviewers want to know you understand the trade-offs:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;async pipe&lt;/strong&gt;&amp;nbsp;:best default; auto-unsubscribes when component destroys&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;takeUntilDestroyed()&lt;/strong&gt; (Angular 16+) : inject DestroyRef, cleanest modern approach&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;takeUntil(this.destroy$)&lt;/strong&gt; with a Subject + ngOnDestroy&amp;nbsp; older but widely used&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;take(1)&lt;/strong&gt;&amp;nbsp; for one-shot observables like single HTTP calls&lt;/li&gt;
&lt;/ul&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;// Modern Angular 16+ approach
private destroyRef = inject(DestroyRef);

ngOnInit() {
  this.data$.pipe(
    takeUntilDestroyed(this.destroyRef)
  ).subscribe(data =&amp;gt; this.data = data);
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Avoid:&lt;/strong&gt; Storing subscriptions in arrays and manually looping to unsubscribe&amp;nbsp; it&#39;s a code smell at senior level.&lt;/p&gt;

&lt;h4&gt;Q: What is the difference between Subject, BehaviorSubject, ReplaySubject, and AsyncSubject?&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Type&lt;/th&gt;
      &lt;th&gt;Initial Value&lt;/th&gt;
      &lt;th&gt;Emits to Late Subscribers&lt;/th&gt;
      &lt;th&gt;Best Use Case&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Subject&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;None&lt;/td&gt;
      &lt;td&gt;Nothing (missed emissions)&lt;/td&gt;
      &lt;td&gt;Event bus, one-time notifications&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;BehaviorSubject&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Requires one&lt;/td&gt;
      &lt;td&gt;Latest value immediately&lt;/td&gt;
      &lt;td&gt;Current state (user auth, theme)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;ReplaySubject(n)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;None&lt;/td&gt;
      &lt;td&gt;Last &lt;em&gt;n&lt;/em&gt; values&lt;/td&gt;
      &lt;td&gt;Caching recent events&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;AsyncSubject&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;None&lt;/td&gt;
      &lt;td&gt;Only last value, on complete&lt;/td&gt;
      &lt;td&gt;HTTP-like single-result operations&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;em&gt;&quot;I use BehaviorSubject in services to hold application state&amp;nbsp; it acts like a mini-store. The &lt;code&gt;.value&lt;/code&gt; getter is convenient but I caution junior devs not to over-rely on it synchronously.&quot;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;3. Dependency Injection&lt;/h3&gt;

&lt;h4&gt;Q: What are the different DI providers in Angular and when do you use each?&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;providedIn: &#39;root&#39;&lt;/strong&gt;&amp;nbsp;: app-wide singleton, tree-shakable (preferred default)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;providedIn: &#39;any&#39;&lt;/strong&gt;&amp;nbsp;: separate instance per lazy-loaded module&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Module-level providers: []&lt;/strong&gt;&amp;nbsp;: scoped to that module and its children&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Component-level providers: []&lt;/strong&gt;&amp;nbsp;: new instance per component instance (great for stateful form wizards)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;inject() function&lt;/strong&gt;&amp;nbsp;: use DI outside constructors, in factory functions, functional guards, and interceptors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Interview trap:&lt;/strong&gt; &lt;em&gt;&quot;What happens if you provide the same service in both root and a lazy module?&quot;&lt;/em&gt; → The lazy module gets its &lt;strong&gt;own instance&lt;/strong&gt;, breaking singleton behavior. This is a real production bug that&#39;s hard to trace.&lt;/p&gt;

&lt;h4&gt;Q: Explain hierarchical dependency injection. How does it work in practice?&lt;/h4&gt;

&lt;p&gt;Angular has a &lt;strong&gt;tree of injectors&lt;/strong&gt; mirroring the component tree. When a component requests a token, Angular walks &lt;strong&gt;up&lt;/strong&gt; the injector tree until it finds a provider. This means:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Child components inherit parent services automatically&lt;/li&gt;
  &lt;li&gt;Providing a service at the component level &lt;strong&gt;shadows&lt;/strong&gt; the higher-level one&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;@SkipSelf()&lt;/code&gt; tells Angular to skip the current injector and look up the tree&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;@Host()&lt;/code&gt; limits resolution to the current component&#39;s host element injector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical use:&lt;/strong&gt; A multi-step form wizard where each step is a child component — provide a &lt;code&gt;FormWizardService&lt;/code&gt; at the wizard component level so all steps share the &lt;strong&gt;same instance&lt;/strong&gt; without polluting the root injector.&lt;/p&gt;

&lt;h3&gt;4. Performance Optimization&lt;/h3&gt;

&lt;h4&gt;Q: How would you optimize an Angular app&#39;s initial load time for Core Web Vitals?&lt;/h4&gt;

&lt;p&gt;A 10-year answer is systematic, not just a list of buzzwords:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Lazy load feature modules / standalone routes&lt;/strong&gt;&amp;nbsp; only load what the current route needs&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Preloading strategy&lt;/strong&gt;&amp;nbsp;&amp;nbsp;PreloadAllModules or custom strategy to preload likely routes on idle&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SSR with Angular Universal + Hydration&lt;/strong&gt;&amp;nbsp; ship pre-rendered HTML for LCP wins&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;@defer blocks (Angular 17+)&lt;/strong&gt;&amp;nbsp; defer heavy components until viewport or interaction&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bundle analysis&lt;/strong&gt;&amp;nbsp;&amp;nbsp;&lt;code&gt;webpack-bundle-analyzer&lt;/code&gt; or ng build --stats-json to find bloat&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;NgOptimizedImage directive&lt;/strong&gt;&amp;nbsp; automatic lazy loading, srcset, and LCP prioritization&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;HTTP/2 + brotli compression&lt;/strong&gt; on the server side&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;Q: An Angular app with a 50,000-item list is causing browser freezes. Walk me through your full optimization approach.&lt;/h4&gt;

&lt;p&gt;A senior engineer gives a layered answer:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Virtual scrolling&lt;/strong&gt; via @angular/cdk/scrolling&amp;nbsp; only render visible items in the DOM&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;trackBy function&lt;/strong&gt; in *ngFor to avoid full list re-renders on reference changes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;OnPush change detection&lt;/strong&gt; on list item components&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Web Workers&lt;/strong&gt; for any heavy data transformation off the main thread&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pagination or infinite scroll&lt;/strong&gt; at the data level before it ever reaches the component&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Q: What is @defer in Angular 17+ and how does it change lazy loading?&lt;/h4&gt;

&lt;p&gt;@defer is a template-level declarative lazy loading mechanism. Unlike route-level lazy loading, &lt;code&gt;@defer&lt;/code&gt; defers &lt;strong&gt;individual components&lt;/strong&gt; within a template based on triggers:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;@defer (on viewport) {
  &amp;lt;heavy-analytics-chart /&amp;gt;
} @placeholder {
  &amp;lt;div class=&quot;skeleton-loader&quot;&amp;gt;&amp;lt;/div&amp;gt;
} @loading (minimum 300ms) {
  &amp;lt;spinner /&amp;gt;
} @error {
  &amp;lt;p&amp;gt;Failed to load chart.&amp;lt;/p&amp;gt;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Triggers include: &lt;code&gt;on idle&lt;/code&gt;, &lt;code&gt;on viewport&lt;/code&gt;, &lt;code&gt;on interaction&lt;/code&gt;, &lt;code&gt;on hover&lt;/code&gt;, &lt;code&gt;on timer(2s)&lt;/code&gt;, and &lt;code&gt;when condition&lt;/code&gt;. Before &lt;code&gt;@defer&lt;/code&gt;, you&#39;d hack this with &lt;code&gt;IntersectionObserver&lt;/code&gt; + dynamic component loading. Now it&#39;s first-class syntax.&lt;/p&gt;

&lt;h3&gt;5. State Management&lt;/h3&gt;

&lt;h4&gt;Q: How do you decide between NgRx, Akita, Signals + Services, or a simple BehaviorSubject store?&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Approach&lt;/th&gt;
      &lt;th&gt;Best For&lt;/th&gt;
      &lt;th&gt;Trade-off&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;BehaviorSubject service&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Small-medium apps, 1 to 2 devs&lt;/td&gt;
      &lt;td&gt;No time-travel, limited devtools&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Signals + Services&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Modern Angular apps, local/shared state&lt;/td&gt;
      &lt;td&gt;Still maturing for complex async flows&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Akita&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Medium apps, less boilerplate than NgRx&lt;/td&gt;
      &lt;td&gt;Smaller community, fewer updates&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;NgRx&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Large teams, complex state, audit trails&lt;/td&gt;
      &lt;td&gt;High boilerplate, steep learning curve&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;em&gt;&quot;I start with Signals and simple services. I only reach for NgRx when the team is 5+ engineers, state is truly global and complex, or we need time-travel debugging for a critical business workflow.&quot;&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;Q: Explain NgRx Effects. What problem do they solve and what&#39;s the most common production mistake?&lt;/h4&gt;

&lt;p&gt;Effects handle &lt;strong&gt;side effects&lt;/strong&gt;&amp;nbsp;async operations like HTTP calls, WebSocket connections, or analytics events&amp;nbsp; outside of reducers, which must stay as pure functions.&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;loadProducts$ = createEffect(() =&amp;gt;
  this.actions$.pipe(
    ofType(ProductActions.loadProducts),
    switchMap(() =&amp;gt;
      this.productService.getAll().pipe(
        map(products =&amp;gt; ProductActions.loadProductsSuccess({ products })),
        catchError(error =&amp;gt; of(ProductActions.loadProductsFailure({ error })))
      )
    )
  )
);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Most common production mistake:&lt;/strong&gt; Forgetting &lt;code&gt;catchError&lt;/code&gt; &lt;em&gt;inside&lt;/em&gt; the &lt;code&gt;switchMap&lt;/code&gt;. If the error reaches the outer pipe, the effect &lt;strong&gt;completes permanently&lt;/strong&gt; and never handles actions again. This is a silent production bug that&#39;s extremely hard to reproduce.&lt;/p&gt;

&lt;h3&gt;6. Architecture &amp;amp; Design Patterns&lt;/h3&gt;

&lt;h4&gt;Q: How would you structure a large-scale Angular application to stay maintainable over 3–5 years?&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Feature-based folder structure&lt;/strong&gt; (not type-based)&amp;nbsp; features are self-contained with their own components, services, and routes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lazy-loaded feature modules / standalone components&lt;/strong&gt; to keep the initial bundle small&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Smart/dumb component pattern&lt;/strong&gt;&amp;nbsp; containers handle state, presentational components are pure and use OnPush universally&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shared library layer&lt;/strong&gt;&amp;nbsp; reusable UI components, utilities, and models in a libs folder (especially in Nx monorepos)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Strong module boundaries&lt;/strong&gt;&amp;nbsp; enforced via ESLint rules or Nx project graph to prevent circular dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Q: How would you implement a micro-frontend architecture with Angular?&lt;/h4&gt;

&lt;p&gt;The standard approach uses &lt;strong&gt;Webpack Module Federation&lt;/strong&gt; via @angular-architects/module-federation:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A &lt;strong&gt;shell app&lt;/strong&gt; that owns routing and shared layout&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Remote apps&lt;/strong&gt; that expose components via ModuleFederationPlugin&lt;/li&gt;
  &lt;li&gt;The shell dynamically loads remotes at runtime using loadRemoteModule()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key challenges: &lt;strong&gt;shared dependency versioning&lt;/strong&gt;, cross-boundary state via an event bus (not direct service calls), and independent deployment via manifest URLs. Honest senior answer: &lt;em&gt;&quot;Micro-frontends solve an organizational problem, not a technical one. I push back when teams want them for purely technical reasons&amp;nbsp; the operational overhead is significant.&quot;&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;Q: How would you migrate a large NgModule-based app to standalone components without breaking production?&lt;/h4&gt;

&lt;p&gt;The key is &lt;strong&gt;incremental migration&lt;/strong&gt;, not a big-bang rewrite:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Enable standalone APIs per component, not all at once&amp;nbsp; start with leaf components&lt;/li&gt;
  &lt;li&gt;Use bootstrapApplication() for the root app&lt;/li&gt;
  &lt;li&gt;Replace module imports with direct imports: [] on standalone components&lt;/li&gt;
  &lt;li&gt;Gradually retire NgModule declarations module-by-module&lt;/li&gt;
  &lt;li&gt;Use Angular&#39;s automatic migration schematic: ng generate @angular/core:standalone&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;7. Angular Signals&lt;/h3&gt;

&lt;h4&gt;Q: What are Angular Signals and how do they change the reactivity model compared to RxJS?&lt;/h4&gt;

&lt;p&gt;Signals (stable in Angular 17+) are &lt;strong&gt;synchronous, fine-grained reactive primitives&lt;/strong&gt; that don&#39;t rely on Zone.js. Unlike RxJS observables, Signals are always synchronous and don&#39;t require subscription management:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;// Signal-based counter
count = signal(0);
doubled = computed(() =&amp;gt; this.count() * 2);

increment() {
  this.count.update(c =&amp;gt; c + 1);
}

// Template
&amp;lt;p&amp;gt;Count: {{ count() }}&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Doubled: {{ doubled() }}&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When to use Signals vs RxJS:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Scenario&lt;/th&gt;
      &lt;th&gt;Use&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Local component state&lt;/td&gt;
      &lt;td&gt;Signals&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;HTTP requests, async streams&lt;/td&gt;
      &lt;td&gt;RxJS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Derived/computed values&lt;/td&gt;
      &lt;td&gt;Signals (&lt;code&gt;computed()&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Complex event orchestration&lt;/td&gt;
      &lt;td&gt;RxJS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Shared app state (modern)&lt;/td&gt;
      &lt;td&gt;Signals + Services or NgRx Signals Store&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3&gt;8. Testing at Scale&lt;/h3&gt;

&lt;h4&gt;Q: How do you approach testing strategy for a team of 15+ Angular developers?&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Testing pyramid&lt;/strong&gt;&amp;nbsp; lots of unit tests (component logic, pipes, services), fewer integration tests, minimal E2E&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;TestBed optimization&lt;/strong&gt;&amp;nbsp; avoid compileComponents() in &lt;code&gt;beforeEach&lt;/code&gt; when not needed; use &lt;code&gt;NO_ERRORS_SCHEMA&lt;/code&gt; carefully to isolate units&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cypress or Playwright for E2E&lt;/strong&gt;&amp;nbsp; covering critical user journeys only&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Code coverage gates in CI&lt;/strong&gt;&amp;nbsp; measuring branch coverage, not just line coverage&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MockStore for NgRx&lt;/strong&gt;&amp;nbsp;use provideMockStore({ initialState }) to control state directly in tests&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Q: How do you test a component that uses HttpClient and an NgRx store?&lt;/h4&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;TestBed.configureTestingModule({
  imports: [HttpClientTestingModule],
  providers: [
    provideMockStore({ initialState: { products: [] } }),
&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/1310830413272703149/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2026/02/angular-interview-questions-for-10-years-experience.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/1310830413272703149'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/1310830413272703149'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2026/02/angular-interview-questions-for-10-years-experience.html' title='Angular Interview Questions &amp; Answers for 10 Years Experience (2026)'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-1077099476329753227</id><published>2026-02-25T21:26:00.008+05:30</published><updated>2026-02-27T15:06:43.832+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>Conditional Rendering in Angular: *ngIf, @if, and ngClass</title><content type='html'>&lt;p style=&quot;text-align: left;&quot;&gt;If you&#39;ve been building Angular apps for any amount of time, you&#39;ve run into the need to show or hide elements based on some condition&amp;nbsp; a loading spinner, an error message, a user-specific button. Angular gives you powerful tools to handle all of this directly in your templates.&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;In this guide, we&#39;ll walk through the classic &lt;strong&gt;*ngIf&lt;/strong&gt; directive, the modern &lt;strong&gt;@if&lt;/strong&gt; block syntax introduced in Angular 17, and how &lt;strong&gt;ngClass&lt;/strong&gt; fits into the picture.&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8VZ5mgdG8mv7oAUkSezs-8BrogoOx8M2AsEFWiYPQV5_zY1mzJGvIo8suo4lNIyQA2u1_usorRgv_jowjKXEnrTj29pRU3catVDealNr3NFRz73Wa5j39OGjMhRRP5z6H3NBkcBvnIh8ptUnOZChBKkbodixU5-8ThsGUFV4cqx0N-8iPNT7LojxkHCQd/s948/conditional%20rendering%20angular.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;408&quot; data-original-width=&quot;948&quot; height=&quot;276&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8VZ5mgdG8mv7oAUkSezs-8BrogoOx8M2AsEFWiYPQV5_zY1mzJGvIo8suo4lNIyQA2u1_usorRgv_jowjKXEnrTj29pRU3catVDealNr3NFRz73Wa5j39OGjMhRRP5z6H3NBkcBvnIh8ptUnOZChBKkbodixU5-8ThsGUFV4cqx0N-8iPNT7LojxkHCQd/w640-h276/conditional%20rendering%20angular.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;

&lt;h3&gt;What Is *ngIf and Why Does It Matter?&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;*ngIf&lt;/strong&gt; is a &lt;strong&gt;structural directive&lt;/strong&gt;&amp;nbsp;meaning it physically adds or removes elements from the DOM, not just hides them with CSS. That&#39;s a meaningful difference. When an element is removed from the DOM, Angular also destroys its component tree, freeing up memory. This makes &lt;strong&gt;*ngIf&lt;/strong&gt; more performant than &lt;strong&gt;display: none&lt;/strong&gt; for large or deeply nested UIs.&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;&amp;lt;p *ngIf=&quot;isLoggedIn&quot;&amp;gt;Welcome back, {{ username }}!&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If &lt;strong&gt;isLoggedIn&lt;/strong&gt; is &lt;strong&gt;false&lt;/strong&gt;, that &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag doesn&#39;t exist in the DOM at all&amp;nbsp; Angular never renders it.&lt;/p&gt;

&lt;h3&gt;Handling the Else Case&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;else&lt;/strong&gt; branch uses an &lt;code&gt;&amp;lt;ng-template&amp;gt;&lt;/code&gt; with a template reference variable. It&#39;s a little verbose, but it gets the job done:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;&amp;lt;div *ngIf=&quot;isLoading; else content&quot;&amp;gt;Loading...&amp;lt;/div&amp;gt;
&amp;lt;ng-template #content&amp;gt;
  &amp;lt;p&amp;gt;Data loaded successfully!&amp;lt;/p&amp;gt;
&amp;lt;/ng-template&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;strong&gt;#content&lt;/strong&gt; reference links the template to the &lt;strong&gt;else&lt;/strong&gt; clause. One thing to watch out for: if that reference doesn&#39;t exist or is misspelled, Angular throws an &lt;strong&gt;NG01352&lt;/strong&gt; error at runtime. Always double-check your template variable names.&lt;/p&gt;

&lt;h3&gt;Simulating else if With *ngIf&lt;/h3&gt;

&lt;p&gt;Angular&#39;s &lt;strong&gt;*ngIf&lt;/strong&gt; doesn&#39;t have a native &lt;strong&gt;else if&lt;/strong&gt;. The workaround is to nest another &lt;strong&gt;*ngIf&lt;/strong&gt; inside an &lt;code&gt;&amp;lt;ng-template&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;&amp;lt;ng-container *ngIf=&quot;condition1; then block1 else block2&quot;&amp;gt;&amp;lt;/ng-container&amp;gt;

&amp;lt;ng-template #block1&amp;gt;Content for condition1&amp;lt;/ng-template&amp;gt;
&amp;lt;ng-template #block2&amp;gt;
  &amp;lt;div *ngIf=&quot;condition2&quot;&amp;gt;Content for condition2&amp;lt;/div&amp;gt;
  &amp;lt;div *ngIf=&quot;!condition2&quot;&amp;gt;Fallback content&amp;lt;/div&amp;gt;
&amp;lt;/ng-template&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It works, but it gets messy fast. That&#39;s exactly the pain point Angular 17 addresses head-on.&lt;/p&gt;

&lt;h3&gt;Angular 17+ : Enter @if, @else if, and @else&lt;/h3&gt;

&lt;p&gt;Angular 17 shipped a completely revamped control flow syntax built directly into the compiler. No more &lt;strong&gt;*ngIf&lt;/strong&gt;. No more &lt;code&gt;&amp;lt;ng-template&amp;gt;&lt;/code&gt;. Just clean, readable blocks that look and feel like real control flow:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;@if (user.role === &#39;admin&#39;) {
  &amp;lt;button&amp;gt;Edit Settings&amp;lt;/button&amp;gt;
} @else if (user.role === &#39;editor&#39;) {
  &amp;lt;button&amp;gt;Draft Post&amp;lt;/button&amp;gt;
} @else {
  &amp;lt;p&amp;gt;Guest users cannot perform actions.&amp;lt;/p&amp;gt;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a massive improvement in developer experience. Native &lt;strong&gt;@else if&lt;/strong&gt; support alone eliminates a ton of awkward nesting.&lt;/p&gt;

&lt;h4&gt;Before vs. After&lt;/h4&gt;
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Old Syntax (Angular ≤16)&lt;/th&gt;
      &lt;th&gt;New Syntax (Angular 17+)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;*ngIf=&quot;isLoggedIn; else loggedOut&quot;+ &amp;lt;ng-template&amp;gt;&lt;/td&gt;
      &lt;td&gt;@if (isLoggedIn) { ... } @else { ... }&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;No native &lt;strong&gt;else if&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;@else if&lt;/strong&gt; works natively&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt; Requires &lt;strong&gt;NgIf&lt;/strong&gt; import&lt;/td&gt;
      &lt;td&gt;Built into the compiler — no import needed&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Verbose, boilerplate-heavy&lt;/td&gt;
      &lt;td&gt;Clean, readable blocks&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4&gt;Type Narrowing Is Now Built In&lt;/h4&gt;

&lt;p&gt;One underrated benefit of the new syntax is TypeScript-aware type narrowing. Inside an &lt;strong&gt;@if&lt;/strong&gt; block, Angular&#39;s compiler knows the type has been checked:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;// component.ts
user: User | null = null;

// template
@if (user) {
  &amp;lt;p&amp;gt;{{ user.name }}&amp;lt;/p&amp;gt;  &amp;lt;!-- TypeScript knows user is non-null here --&amp;gt;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;No more &lt;strong&gt;?.&lt;/strong&gt; safe navigation operators sprinkled everywhere just to avoid null errors.&lt;/p&gt;

&lt;h3&gt;Pairing with @for and @empty&lt;/h3&gt;

&lt;p&gt;The new control flow syntax doesn&#39;t stop at conditionals. It pairs naturally with &lt;strong&gt;@for&lt;/strong&gt; loops and an &lt;strong&gt;@empty&lt;/strong&gt; block for handling empty lists:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;@for (item of items; track item.id) {
  &amp;lt;div&amp;gt;{{ item.name }}&amp;lt;/div&amp;gt;
} @empty {
  &amp;lt;p&amp;gt;No items found.&amp;lt;/p&amp;gt;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Previously, you&#39;d combine &lt;strong&gt;*ngFor&lt;/strong&gt; with an &lt;strong&gt;*ngIf&lt;/strong&gt; check for empty state. Now it&#39;s a single, self-contained block.&lt;/p&gt;

&lt;h3&gt;Dynamic Styling with ngClass&lt;/h3&gt;

&lt;p&gt;While &lt;strong&gt;*ngIf&lt;/strong&gt; and &lt;strong&gt;@if&lt;/strong&gt; control whether an element exists, &lt;strong&gt;ngClass&lt;/strong&gt; controls how it looks. They complement each other well:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;&amp;lt;!-- Apply a single class conditionally --&amp;gt;
&amp;lt;div [ngClass]=&quot;{ &#39;active&#39;: isActive }&quot;&amp;gt;...&amp;lt;/div&amp;gt;

&amp;lt;!-- Apply multiple classes based on different conditions --&amp;gt;
&amp;lt;div [ngClass]=&quot;{ &#39;success&#39;: isSuccess, &#39;error&#39;: hasError }&quot;&amp;gt;...&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A common real-world pattern: use &lt;strong&gt;@if&lt;/strong&gt; to decide &lt;em&gt;if&lt;/em&gt; a component renders, and &lt;strong&gt;ngClass&lt;/strong&gt; to handle its visual states once it does.&lt;/p&gt;

&lt;h3&gt;Common Real-World Use Cases&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Loading states&lt;/strong&gt;&amp;nbsp;: Show a spinner while data fetches, swap it for content when ready&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Error handling&lt;/strong&gt;&amp;nbsp;: Display alert banners only when an error flag is set&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Authentication gates&lt;/strong&gt;&amp;nbsp;: Show Login/Logout buttons based on session state&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Role-based UI&lt;/strong&gt;&amp;nbsp;: Render different controls for admins, editors, and viewers using &lt;strong&gt;@else if&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Best Practices Worth Following&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Keep complex logic out of templates.&lt;/strong&gt; If your condition has more than one or two checks, move it to a getter in the component:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;// component.ts
get isUserValid(): boolean {
  return this.user &amp;amp;&amp;amp; this.user.isActive;
}

// template
&amp;lt;div *ngIf=&quot;isUserValid&quot;&amp;gt;...&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Use &lt;code&gt;&amp;lt;ng-container&amp;gt;&lt;/code&gt; when you need a conditional wrapper that shouldn&#39;t add a real DOM node. It&#39;s invisible in the rendered output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Start migrating to @if today.&lt;/strong&gt; Angular plans to phase out structural directives in favor of the new block syntax. There&#39;s an automatic migration command:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;ng generate @angular/core:control-flow&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This migrates your entire project&#39;s &lt;strong&gt;*ngIf&lt;/strong&gt;, &lt;strong&gt;*ngFor&lt;/strong&gt;, and &lt;strong&gt;*ngSwitch&lt;/strong&gt; to the new syntax in one shot.&lt;/p&gt;

&lt;h3&gt;Troubleshooting: NG01352 Error&lt;/h3&gt;

&lt;p&gt;If Angular throws &lt;strong&gt;NG01352&lt;/strong&gt;, it can&#39;t find the &lt;code&gt;&amp;lt;ng-template&amp;gt;&lt;/code&gt; referenced in your &lt;strong&gt;*ngIf else&lt;/strong&gt; clause. The fix is usually one of two things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The &lt;strong&gt;#templateName&lt;/strong&gt; variable is misspelled or missing&lt;/li&gt;
  &lt;li&gt;The template is defined inside another structural directive&amp;nbsp; Angular can&#39;t reach it from there&lt;/li&gt;
&lt;/ul&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-left: 4px solid rgb(233, 30, 99); border-radius: 4px; font-size: 14px; line-height: 1.6; overflow: auto; padding: 16px;&quot;&gt;&lt;code&gt;&amp;lt;!-- This breaks if #missingTemplate doesn&#39;t exist --&amp;gt;
&amp;lt;div *ngIf=&quot;condition; else missingTemplate&quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Quick Reference&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Feature&lt;/th&gt;
      &lt;th&gt;*ngIf (≤ Angular 16)&lt;/th&gt;
      &lt;th&gt;@if (Angular 17+)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Basic conditional&lt;/td&gt;
      &lt;td&gt;✅&lt;/td&gt;
      &lt;td&gt;✅&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Else support&lt;/td&gt;
      &lt;td&gt;✅ via &amp;lt;ng-template&amp;gt;&lt;/td&gt;
      &lt;td&gt;✅ natively&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Else if&lt;/td&gt;
      &lt;td&gt;❌ (workaround needed)&lt;/td&gt;
      &lt;td&gt;✅ natively&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Type narrowing&lt;/td&gt;
      &lt;td&gt;❌&lt;/td&gt;
      &lt;td&gt;✅&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Import required&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;NgIf&lt;/strong&gt; from @angular/common&lt;/td&gt;
      &lt;td&gt;None&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Boilerplate&lt;/td&gt;
      &lt;td&gt;High&lt;/td&gt;
      &lt;td&gt;Minimal&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Whether you&#39;re maintaining an older codebase or starting fresh, understanding both &lt;strong&gt;*ngIf&lt;/strong&gt; and the new &lt;strong&gt;@if&lt;/strong&gt; syntax gives you the full picture of Angular&#39;s conditional rendering story. The new block syntax is clearly the direction Angular is heading&amp;nbsp; cleaner templates, better type safety, and noticeably faster rendering. Start with the migration command, and you&#39;ll be surprised how quick the switch is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Further Reading:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://angular.dev/guide/templates/control-flow&quot; target=&quot;_blank&quot;&gt;Angular Control Flow Official Docs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://blog.angular-university.io/angular-ngif/&quot; target=&quot;_blank&quot;&gt;NgIf Complete Guide — Angular University&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://angular.dev/api/common/NgClass&quot; target=&quot;_blank&quot;&gt;Angular ngClass Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/1077099476329753227/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2026/02/conditional-rendering-in-angular-ngif.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/1077099476329753227'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/1077099476329753227'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2026/02/conditional-rendering-in-angular-ngif.html' title='Conditional Rendering in Angular: *ngIf, @if, and ngClass'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8VZ5mgdG8mv7oAUkSezs-8BrogoOx8M2AsEFWiYPQV5_zY1mzJGvIo8suo4lNIyQA2u1_usorRgv_jowjKXEnrTj29pRU3catVDealNr3NFRz73Wa5j39OGjMhRRP5z6H3NBkcBvnIh8ptUnOZChBKkbodixU5-8ThsGUFV4cqx0N-8iPNT7LojxkHCQd/s72-w640-h276-c/conditional%20rendering%20angular.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-8894407284157596739</id><published>2026-02-20T15:25:00.008+05:30</published><updated>2026-02-27T15:10:10.356+05:30</updated><title type='text'>Top Angular coding Interview Questions and Answers (2026 Edition)</title><content type='html'>&lt;p style=&quot;text-align: left;&quot;&gt;Angular interviews can feel overwhelming&amp;nbsp; there&#39;s a lot to cover and interviewers love going deep. The good news is that most questions circle around the same core ideas.&amp;nbsp;&lt;/p&gt;&lt;p&gt;Work through this list, understand the &lt;em&gt;why&lt;/em&gt; behind each answer, and you&#39;ll walk in confident.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6OXMlQyp_r8nvR3UdnA8uGZlwmkQDjfsReLZqcOJqyfAXXZdkN-gJ84NCBg8edNpZxhW-J5Q78X1Tv3ugwZnI4MWPxyfWYUe-6053DTyCsbYGdidY_NDu34jVe9PP_lCYsaBGQmsaQDbrEDCJQD_yMlHohGhtV8ra_f9RSQbEE618zYOeRpFDIJGt3I6H/s906/angular%20coding%20interview%20questions.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;770&quot; data-original-width=&quot;906&quot; height=&quot;340&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6OXMlQyp_r8nvR3UdnA8uGZlwmkQDjfsReLZqcOJqyfAXXZdkN-gJ84NCBg8edNpZxhW-J5Q78X1Tv3ugwZnI4MWPxyfWYUe-6053DTyCsbYGdidY_NDu34jVe9PP_lCYsaBGQmsaQDbrEDCJQD_yMlHohGhtV8ra_f9RSQbEE618zYOeRpFDIJGt3I6H/w400-h340/angular%20coding%20interview%20questions.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;



&lt;h2&gt;Core Angular Concepts&lt;/h2&gt;



&lt;h3&gt;1. What is the difference between AngularJS and Angular?&lt;/h3&gt;

&lt;p&gt;This one comes up a lot, especially if your resume mentions both. AngularJS (v1.x) was built in the early days of single-page apps&amp;nbsp; it used JavaScript, a controller/scope model, and MVC architecture.&lt;/p&gt;&lt;p&gt;&amp;nbsp;Angular (v2 and beyond) is essentially a completely different framework that just kept the name. It was rewritten in TypeScript, shifted to a component-based model, and introduced the Ivy compiler which made apps significantly faster and smaller.&amp;nbsp;&lt;/p&gt;&lt;p&gt;If an interviewer asks this, they&#39;re checking whether you know Angular has a history, not just a present.&lt;/p&gt;

&lt;h3&gt;2. Explain the component lifecycle hooks.&lt;/h3&gt;

&lt;p&gt;Think of lifecycle hooks as Angular tapping you on the shoulder and saying &quot;hey, something just happened to your component want to do something about it?&quot; They fire in this order:&lt;/p&gt;

&lt;ul&gt;

  &lt;li&gt;&lt;strong&gt;ngOnChanges&lt;/strong&gt;&amp;nbsp;:called first, whenever a bound @Input() value changes&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ngOnInit&lt;/strong&gt;&amp;nbsp;: your go-to hook for setup logic; runs once after the first ngOnChanges&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ngDoCheck&lt;/strong&gt;&amp;nbsp;: runs on every change detection cycle; use it carefully, it fires a lot&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ngAfterContentInit / ngAfterContentChecked:&lt;/strong&gt;&amp;nbsp;triggered after Angular projects external content via ng-content&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ngAfterViewInit / ngAfterViewChecked:&lt;/strong&gt;&amp;nbsp;triggered after the component&#39;s own view and all child views are fully rendered&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ngOnDestroy:&lt;/strong&gt;&amp;nbsp;your cleanup hook; always unsubscribe from observables here to avoid memory leaks&lt;/li&gt;

&lt;/ul&gt;



&lt;h3&gt;3. What is Dependency Injection (DI) in Angular?&lt;/h3&gt;

&lt;p&gt;DI sounds fancy but the idea is simple: instead of a class creating its own dependencies (like a service), Angular hands them over from the outside. This makes your code easier to test and swap out. You tell Angular a service is injectable with @Injectable(), and using providedIn: &#39;root&#39; creates one shared instance across the whole app.&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;@Injectable({ providedIn: &#39;root&#39; })

export class DataService {

  getData() { ... }

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



&lt;h2&gt;Directives &amp;amp; Templates&lt;/h2&gt;



&lt;h3&gt;4. What is the difference between structural and attribute directives?&lt;/h3&gt;

&lt;p&gt;The easiest way to remember this: structural directives change the &lt;em&gt;shape&lt;/em&gt; of the DOM (they add or remove elements), while attribute directives change the &lt;em&gt;look or behavior&lt;/em&gt; of something already there.&lt;/p&gt;

&lt;ul&gt;

  &lt;li&gt;&lt;strong&gt;Structural&lt;/strong&gt;&amp;nbsp;:&amp;nbsp;*ngIf removes an element from the DOM entirely; *ngFor stamps out multiple elements&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Attribute:&amp;nbsp;&lt;/strong&gt;&lt;code&gt;ngClass&lt;/code&gt; adds CSS classes; &lt;code&gt;ngStyle&lt;/code&gt; applies inline styles&lt;/li&gt;

&lt;/ul&gt;



&lt;h3&gt;5. How do you create a custom directive?&lt;/h3&gt;

&lt;p&gt;Say you want to highlight any element when the user hovers over it. Instead of writing that logic in every component, you create a directive once and reuse it anywhere. Here&#39;s a real example:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;@Directive({ selector: &#39;[appHighlight]&#39; })

export class HighlightDirective {

  constructor(private el: ElementRef) {}



  @HostListener(&#39;mouseenter&#39;) onMouseEnter() {

    this.el.nativeElement.style.backgroundColor = &#39;yellow&#39;;

  }



  @HostListener(&#39;mouseleave&#39;) onMouseLeave() {

    this.el.nativeElement.style.backgroundColor = &#39;&#39;;

  }

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

&lt;p&gt;Drop it on any element with &lt;code&gt;&amp;lt;p appHighlight&amp;gt;Hover over me&amp;lt;/p&amp;gt;&lt;/code&gt; and it just works.&lt;/p&gt;



&lt;h2&gt;Data Binding &amp;amp; Component Communication&lt;/h2&gt;



&lt;h3&gt;6. What are the types of data binding in Angular?&lt;/h3&gt;

&lt;p&gt;Data binding is how your component class and HTML template talk to each other. Angular gives you four ways to do it:&lt;/p&gt;

&lt;ul&gt;

  &lt;li&gt;&lt;strong&gt;Interpolation&lt;/strong&gt;&amp;nbsp;:&amp;nbsp;{{ value }}&amp;nbsp;simplest way to display data in the template&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Property binding:&amp;nbsp;&lt;/strong&gt;[property]=&quot;value&quot;&amp;nbsp; pushes data from the class into a DOM property&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Event binding&lt;/strong&gt;&amp;nbsp;:&amp;nbsp;(event)=&quot;handler()&quot;&amp;nbsp;listens for user actions and calls a method&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Two-way binding&lt;/strong&gt;&amp;nbsp;:&amp;nbsp;[(ngModel)]=&quot;property&quot;&amp;nbsp; keeps the input and the class in sync; needs FormsModule imported&lt;/li&gt;

&lt;/ul&gt;



&lt;h3&gt;7. How do parent and child components communicate?&lt;/h3&gt;

&lt;p&gt;Component communication is one of those topics that trips up junior developers but becomes second nature once you&#39;ve built a few features.&lt;/p&gt;

&lt;ul&gt;

  &lt;li&gt;&lt;strong&gt;Parent → Child:&lt;/strong&gt; Use @Input() to pass data down from the parent&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Child → Parent:&lt;/strong&gt; Use @Output() withEventEmitter to send events back up&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Sibling / unrelated components:&lt;/strong&gt; A shared service with a Subject or BehaviorSubject is your best friend here&lt;/li&gt;

&lt;/ul&gt;



&lt;h2&gt;RxJS &amp;amp; Observables&lt;/h2&gt;



&lt;h3&gt;8. What is the difference between an Observable and a Promise?&lt;/h3&gt;

&lt;p&gt;Promises and Observables both handle async code, but they behave very differently under the hood. Here&#39;s a quick side-by-side:&lt;/p&gt;

&lt;table style=&quot;border-collapse: collapse; margin: 16px 0px; width: 100%;&quot;&gt;

  &lt;thead&gt;

    &lt;tr style=&quot;background: rgb(240, 240, 240);&quot;&gt;

      &lt;th style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px; text-align: left;&quot;&gt;&lt;/th&gt;

      &lt;th style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px; text-align: left;&quot;&gt;Observable&lt;/th&gt;

      &lt;th style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px; text-align: left;&quot;&gt;Promise&lt;/th&gt;

    &lt;/tr&gt;

  &lt;/thead&gt;

  &lt;tbody&gt;

    &lt;tr&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Execution&lt;/td&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Lazy — nothing runs until you subscribe&lt;/td&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Eager — starts immediately when created&lt;/td&gt;

    &lt;/tr&gt;

    &lt;tr style=&quot;background: rgb(249, 249, 249);&quot;&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Values&lt;/td&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Can emit multiple values over time&lt;/td&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Resolves once with a single value&lt;/td&gt;

    &lt;/tr&gt;

    &lt;tr&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Cancellable&lt;/td&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Yes, just unsubscribe&lt;/td&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;No built-in cancellation&lt;/td&gt;

    &lt;/tr&gt;

    &lt;tr style=&quot;background: rgb(249, 249, 249);&quot;&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Operators&lt;/td&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Huge RxJS operator library&lt;/td&gt;

      &lt;td style=&quot;border: 1px solid rgb(221, 221, 221); padding: 10px;&quot;&gt;Basic chaining with .then()&lt;/td&gt;

    &lt;/tr&gt;

  &lt;/tbody&gt;

&lt;/table&gt;



&lt;h3&gt;9. How do you prevent memory leaks with observables?&lt;/h3&gt;

&lt;p&gt;Forgetting to unsubscribe is one of the most common bugs in Angular apps. The subscription keeps running even after the component is gone, wasting memory. Three solid ways to avoid this:&lt;/p&gt;

&lt;ul&gt;

  &lt;li&gt;Use the async pipe in your template&amp;nbsp; Angular automatically subscribes and unsubscribes for you&lt;/li&gt;

  &lt;li&gt;Use takeUntil with a destroy subject&amp;nbsp; clean and works great for multiple subscriptions&lt;/li&gt;

  &lt;li&gt;Use take(1) when you only need the first emitted value&lt;/li&gt;

&lt;/ul&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;private destroy$ = new Subject&amp;lt;void&amp;gt;();



ngOnInit() {

  this.dataService.getData()

    .pipe(takeUntil(this.destroy$))

    .subscribe(data =&amp;gt; this.data = data);

}



ngOnDestroy() {

  this.destroy$.next();

  this.destroy$.complete();

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



&lt;h3&gt;10. How do you retry an HTTP request on failure?&lt;/h3&gt;

&lt;p&gt;Network requests fail sometimes. RxJS makes it easy to automatically retry before giving up&amp;nbsp; no manual loop needed.&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;this.http.get(url).pipe(

  retry(3),

  catchError(err =&amp;gt; {

    console.error(&#39;Request failed after 3 retries&#39;, err);

    return throwError(() =&amp;gt; err);

  })

).subscribe(data =&amp;gt; console.log(data));&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;Advanced Topics&lt;/h2&gt;



&lt;h3&gt;11. Explain Angular&#39;s change detection strategies.&lt;/h3&gt;

&lt;p&gt;Angular watches for changes in your app and updates the view accordingly. By default it&#39;s aggressive — it checks everything. But you can make it smarter:&lt;/p&gt;

&lt;ul&gt;

  &lt;li&gt;&lt;strong&gt;Default:&lt;/strong&gt; Angular walks the entire component tree on every browser event, timer tick, or async call. Safe, but can be slow in large apps&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;OnPush:&lt;/strong&gt; Angular only re-checks the component when an &lt;code&gt;@Input()&lt;/code&gt; reference changes, an internal event fires, or you manually call &lt;code&gt;ChangeDetectorRef.markForCheck()&lt;/code&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Switching your components to &lt;code&gt;OnPush&lt;/code&gt; is one of the easiest wins for app performance, especially in component-heavy dashboards.&lt;/p&gt;



&lt;h3&gt;12. How does lazy loading work in Angular?&lt;/h3&gt;

&lt;p&gt;Nobody wants to download the entire app upfront. Lazy loading lets Angular fetch feature modules only when a user actually navigates to them, keeping the initial bundle small and load times fast.&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;// app-routing.module.ts

const routes: Routes = [

  {

    path: &#39;dashboard&#39;,

    loadChildren: () =&amp;gt;

      import(&#39;./dashboard/dashboard.module&#39;).then(m =&amp;gt; m.DashboardModule)

  }

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

&lt;p&gt;If you&#39;re using standalone components (Angular 15+), use &lt;code&gt;loadComponent&lt;/code&gt; directly&amp;nbsp; no module needed:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;{

  path: &#39;profile&#39;,

  loadComponent: () =&amp;gt;

    import(&#39;./profile/profile.component&#39;).then(c =&amp;gt; c.ProfileComponent)

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



&lt;h3&gt;13. What is Angular Universal?&lt;/h3&gt;

&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Angular runs in the browser by default, which means search engine crawlers often see a blank page before JavaScript kicks in.&amp;nbsp;&lt;/li&gt;&lt;li&gt;Angular Universal solves this with Server-Side Rendering (SSR)&amp;nbsp; the server builds the full HTML first, sends it to the browser, then Angular takes over.&amp;nbsp;&lt;/li&gt;&lt;li&gt;The result is faster perceived load times and much better SEO. From Angular 17 onwards, SSR ships with the CLI out of the box when you run &lt;code&gt;ng new --ssr&lt;/code&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;



&lt;h2&gt;Performance Optimization&lt;/h2&gt;



&lt;h3&gt;14. How do you optimize *ngFor performance?&lt;/h3&gt;

&lt;p&gt;Without &lt;code&gt;trackBy&lt;/code&gt;, Angular tears down and rebuilds the entire list whenever the data array changes&amp;nbsp; even if only one item changed.&amp;nbsp;&lt;/p&gt;&lt;p&gt;Add &lt;code&gt;trackBy&lt;/code&gt; and Angular knows which items are new, updated, or removed, so it only touches what needs touching.&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;&amp;lt;div *ngFor=&quot;let item of items; trackBy: trackById&quot;&amp;gt;{{ item.name }}&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;trackById(index: number, item: any): number {

  return item.id;

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



&lt;h3&gt;15. What is the difference between pure and impure pipes?&lt;/h3&gt;

&lt;p&gt;This is a subtle but important distinction for performance:&lt;/p&gt;

&lt;ul&gt;

  &lt;li&gt;&lt;strong&gt;Pure pipes:&lt;/strong&gt;&amp;nbsp;Angular only reruns the pipe when the input value actually changes. This is the default and what you want 99% of the time&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Impure pipes&lt;/strong&gt;&amp;nbsp;:Angular reruns them on every change detection cycle, regardless of whether the input changed. They&#39;re powerful but can noticeably slow down your app if overused&lt;/li&gt;

&lt;/ul&gt;



&lt;h2&gt;Angular Signals (v16+)&lt;/h2&gt;



&lt;h3&gt;16. What are Angular Signals and why do they matter?&lt;/h3&gt;

&lt;p&gt;Signals are Angular&#39;s newer approach to reactivity and honestly one of the most exciting recent additions.&amp;nbsp;&lt;/p&gt;&lt;p&gt;Instead of relying on Zone.js to detect changes across the whole app, a signal tracks exactly who depends on it and only notifies those dependents when its value changes.&lt;/p&gt;&lt;p&gt;&amp;nbsp;It&#39;s more predictable and more performant.&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;import { signal, computed, effect } from &#39;@angular/core&#39;;



count = signal(0);

doubled = computed(() =&amp;gt; this.count() * 2);



increment() {

  this.count.update(c =&amp;gt; c + 1);

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

&lt;p&gt;Signals pair perfectly with OnPush components and are the building blocks for Angular&#39;s upcoming Zoneless mode&amp;nbsp; so it&#39;s worth getting comfortable with them now.&lt;/p&gt;



&lt;h2&gt;New Control Flow Syntax (v17+)&lt;/h2&gt;



&lt;h3&gt;17. What is Angular&#39;s new built-in control flow?&lt;/h3&gt;

&lt;p&gt;Angular 17 shipped a new template syntax that replaces the old structural directives. It&#39;s cleaner, easier to read, and adds some useful new features like the @empty block for empty lists. You&#39;ll start seeing this in most new codebases:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;&amp;lt;!-- Replaces *ngIf --&amp;gt;

@if (isLoggedIn) {

  &amp;lt;app-dashboard /&amp;gt;

} @else {

  &amp;lt;app-login /&amp;gt;

}



&amp;lt;!-- Replaces *ngFor, with built-in empty state --&amp;gt;

@for (item of items; track item.id) {

  &amp;lt;li&amp;gt;{{ item.name }}&amp;lt;/li&amp;gt;

} @empty {

  &amp;lt;li&amp;gt;No items found.&amp;lt;/li&amp;gt;

}



&amp;lt;!-- Replaces ngSwitch --&amp;gt;

@switch (status) {

  @case (&#39;active&#39;) { &amp;lt;span&amp;gt;Active&amp;lt;/span&amp;gt; }

  @case (&#39;inactive&#39;) { &amp;lt;span&amp;gt;Inactive&amp;lt;/span&amp;gt; }

  @default { &amp;lt;span&amp;gt;Unknown&amp;lt;/span&amp;gt; }

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



&lt;h2&gt;Testing&lt;/h2&gt;



&lt;h3&gt;18. How do you test a component with dependencies?&lt;/h3&gt;

&lt;p&gt;The key to unit testing in Angular is isolation&amp;nbsp; you want to test the component, not its dependencies. Use &lt;code&gt;TestBed&lt;/code&gt; to set up a minimal testing environment and swap real services with mock versions:&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;beforeEach(() =&amp;gt; {

  TestBed.configureTestingModule({

    declarations: [MyComponent],

    providers: [{ provide: MyService, useClass: MockService }]

  });

  fixture = TestBed.createComponent(MyComponent);

});



it(&#39;should display fetched data&#39;, () =&amp;gt; {

  fixture.detectChanges();

  const compiled = fixture.nativeElement;

  expect(compiled.querySelector(&#39;h1&#39;).textContent).toContain(&#39;Hello&#39;);

});&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;Real-World Scenarios&lt;/h2&gt;



&lt;h3&gt;19. How do you implement a debounced search input?&lt;/h3&gt;

&lt;p&gt;Firing an API call on every single keystroke is a quick way to hammer your backend. A debounced search waits until the user pauses typing before sending the request.&amp;nbsp;&lt;/p&gt;&lt;p&gt;switchMap is the critical piece here&amp;nbsp; it cancels the previous in-flight request if the user types again, so you never process stale results.&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;searchTerm = new FormControl(&#39;&#39;);



ngOnInit() {

  this.searchTerm.valueChanges.pipe(

    debounceTime(300),

    distinctUntilChanged(),

    switchMap(term =&amp;gt; this.searchService.search(term))

  ).subscribe(results =&amp;gt; this.results = results);

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



&lt;h3&gt;20. How do you add authentication headers using an HTTP interceptor?&lt;/h3&gt;

&lt;p&gt;Rather than manually adding an auth token to every single HTTP call, interceptors let you do it once in one place. Every outgoing request passes through your interceptor automatically.&lt;/p&gt;

&lt;pre style=&quot;background: rgb(244, 244, 244); border-radius: 4px; border: 1px solid rgb(221, 221, 221); font-family: monospace; font-size: 14px; line-height: 1.6; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code&gt;@Injectable()

export class AuthInterceptor implements HttpInterceptor {

  constructor(private authService: AuthService) {}



  intercept(req: HttpRequest&amp;lt;any&amp;gt;, next: HttpHandler): Observable&amp;lt;HttpEvent&amp;lt;any&amp;gt;&amp;gt; {

    const token = this.authService.getToken();

    if (token) {

      const authReq = req.clone({

        setHeaders: { Authorization: `Bearer ${token}` }

      });

      return next.handle(authReq);

    }

    return next.handle(req);

  }

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

&lt;p&gt;Wire it up in your providers array: { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }. The &lt;code&gt;multi: true&lt;/code&gt; flag is important&amp;nbsp; it tells Angular you&#39;re adding to a list of interceptors, not replacing them.&lt;/p&gt;



&lt;h2&gt;Common Pitfalls&lt;/h2&gt;



&lt;h3&gt;21. Why does ngModel require FormsModule?&lt;/h3&gt;

&lt;p&gt;ngModel isn&#39;t a built-in HTML attribute&amp;nbsp; it&#39;s an Angular directive that lives inside FormsModule. If you forget to import FormsModule in your module (or in a standalone component&#39;s imports array), Angular simply won&#39;t recognize it and you&#39;ll get a template parse error. It&#39;s a small thing but it trips up a surprising number of developers.&lt;/p&gt;



&lt;h3&gt;22. How do you fix ExpressionChangedAfterItHasBeenCheckedError?&lt;/h3&gt;

&lt;p&gt;This error shows up when something in your code changes a binding value &lt;em&gt;after&lt;/em&gt; Angular has already finished checking it for that cycle. It only appears in development mode, but it&#39;s Angular warning you about a real design issue. Here&#39;s how to deal with it:&lt;/p&gt;

&lt;ul&gt;

  &lt;li&gt;Move the state change into ngAfterViewInit and immediately call ChangeDetectorRef.detectChanges() to trigger another check&lt;/li&gt;

  &lt;li&gt;Wrap the change in setTimeout() to push it to the next event loop tick&amp;nbsp; works but treat it as a last resort&lt;/li&gt;

  &lt;li&gt;The cleanest fix is to restructure your code so the value is set before change detection starts, ideally in ngOnInit&lt;/li&gt;

&lt;/ul&gt;



&lt;h2&gt;Before You Walk Into That Interview&lt;/h2&gt;

&lt;p&gt;Technical knowledge only gets you so far&amp;nbsp; interviewers also want to see how you think. A few things worth focusing on in your last few days of prep:&lt;/p&gt;

&lt;ul&gt;

  &lt;li&gt;&lt;strong&gt;RxJS operators:&lt;/strong&gt;&amp;nbsp;get comfortable with map,filter, switchMap,mergeMap, combineLatest, catchError, and debounceTime; know when to use each&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Signals vs. RxJS:&lt;/strong&gt;&amp;nbsp;understand the difference and when one makes more sense than the other&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Change detection:&lt;/strong&gt;&amp;nbsp;be able to explain Default vs OnPush and why Signals are moving Angular away from Zone.js&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Standalone components:&lt;/strong&gt;&amp;nbsp;know how to bootstrap an app and declare imports without NgModules&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Build something small:&lt;/strong&gt;&amp;nbsp;a search-as-you-type feature, a simple auth flow, or a todo list will make these concepts stick far better than just reading about them&lt;/li&gt;&lt;/ul&gt;

</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/8894407284157596739/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2026/02/top-angular-coding-interview-questions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/8894407284157596739'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/8894407284157596739'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2026/02/top-angular-coding-interview-questions.html' title='Top Angular coding Interview Questions and Answers (2026 Edition)'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6OXMlQyp_r8nvR3UdnA8uGZlwmkQDjfsReLZqcOJqyfAXXZdkN-gJ84NCBg8edNpZxhW-J5Q78X1Tv3ugwZnI4MWPxyfWYUe-6053DTyCsbYGdidY_NDu34jVe9PP_lCYsaBGQmsaQDbrEDCJQD_yMlHohGhtV8ra_f9RSQbEE618zYOeRpFDIJGt3I6H/s72-w400-h340-c/angular%20coding%20interview%20questions.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-4797478984794367458</id><published>2025-08-31T23:18:00.008+05:30</published><updated>2026-02-11T22:31:02.289+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><category scheme="http://www.blogger.com/atom/ns#" term="Javascript interview questions"/><title type='text'> 40+ Angular and JavaScript Interview Questions</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;lets discuss 40 + frequently asked angular and JavaScript interview questions&amp;nbsp; and answers&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLZGicKRnQT_QU_Y2Vn4eHYhgIp732vnrvd6J-5R6shD_QjFOnTkfhSZAnGOShD0YWz6LbqMKRpr_oythkTm0_zfrzJ3T6jyqi-Fg_evvtT5boDaD1Vzu23Qkh6Vx9GIon5T7FpgaCUghzYAorpPZQXikuy8Q4lERFGD89T5vFky55oThw3QYoN_XtM5Br/s594/angular%20and%20javascript%20interview%20questiosn%20and%20answers.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;396&quot; data-original-width=&quot;594&quot; height=&quot;266&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLZGicKRnQT_QU_Y2Vn4eHYhgIp732vnrvd6J-5R6shD_QjFOnTkfhSZAnGOShD0YWz6LbqMKRpr_oythkTm0_zfrzJ3T6jyqi-Fg_evvtT5boDaD1Vzu23Qkh6Vx9GIon5T7FpgaCUghzYAorpPZQXikuy8Q4lERFGD89T5vFky55oThw3QYoN_XtM5Br/w400-h266/angular%20and%20javascript%20interview%20questiosn%20and%20answers.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;JavaScript Interview Questions&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;1. What are closures in JavaScript?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Closures allow an inner function to remember variables from its outer function even after the outer function has finished executing.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;function outer() {&lt;/p&gt;&lt;p&gt;&amp;nbsp; let count = 0;&lt;/p&gt;&lt;p&gt;&amp;nbsp; return function inner() {&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; count++;&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; return count;&lt;/p&gt;&lt;p&gt;&amp;nbsp; };&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;const counter = outer();&lt;/p&gt;&lt;p&gt;console.log(counter()); // 1&lt;/p&gt;&lt;p&gt;console.log(counter()); // 2&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Closures power callbacks, event listeners, and async programming all of which are used in Angular.&lt;/li&gt;&lt;/ul&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;2. Difference between var, let, and const?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;var : functionscoped, can be redeclared.&lt;/li&gt;&lt;li&gt;let : blockscoped, reassignable.&lt;/li&gt;&lt;li&gt;const: blockscoped, cannot be reassigned.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;3. What is event delegation?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Instead of attaching event listeners to multiple child elements, you attach one to the parent. The event bubbles up and gets handled at the parent level.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&amp;nbsp;Saves memory and improves performance—super important in large apps.&lt;/li&gt;&lt;/ul&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;4. Difference between == and ===?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;==&amp;nbsp; loose equality (performs type conversion).&lt;/li&gt;&lt;li&gt;===&amp;nbsp; strict equality (compares type + value).&lt;/li&gt;&lt;li&gt;Always use === to avoid bugs.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;5. What is the event loop in JavaScript?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;The event loop is what makes JavaScript asynchronous. It constantly checks the call stack and the callback queue, allowing nonblocking code execution.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;6. What are Promises and async/await?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Promise :represents a future value (resolved or rejected).&lt;/li&gt;&lt;li&gt;async/await: syntactic sugar for writing cleaner asynchronous code.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;async function fetchData() {&lt;/p&gt;&lt;p&gt;&amp;nbsp; const res = await fetch(&#39;/api/data&#39;);&lt;/p&gt;&lt;p&gt;&amp;nbsp; return res.json();&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;7. What is hoisting?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Hoisting means JavaScript moves variable and function declarations to the top of their scope before code execution.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;8. Difference between null and undefined?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;null : intentional “no value.”&lt;/li&gt;&lt;li&gt;undefined : variable declared but not assigned.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;9. What are arrow functions, and how are they different?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Arrow functions don’t have their own this. They inherit it from their surrounding scope.&lt;/li&gt;&lt;li&gt;Useful in Angular callbacks and RxJS streams.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;10. What is the difference between synchronous and asynchronous JavaScript?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Synchronous : one task at a time.&lt;/li&gt;&lt;li&gt;Asynchronous : tasks can run in the background (timers, API calls).&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;11. What is a callback function?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;A callback is a function passed as an argument and executed later.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;12. What is a prototype in JavaScript?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Every JS object has a prototype, which enables inheritance. Angular’s TypeScript classes compile down to prototypebased JS.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;13. What are higherorder functions?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Functions that take other functions as arguments or return them.&lt;/li&gt;&lt;li&gt;Examples: map(), filter(), reduce().&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;14. Explain deep copy vs shallow copy.&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Shallow copy: copies only toplevel properties.&lt;/li&gt;&lt;li&gt;Deep copy&amp;nbsp; means copies nested objects as well.&lt;/li&gt;&lt;/ul&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Angular Interview Questions&lt;/h3&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;15. What is Angular, and how does it differ from AngularJS?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;AngularJS (1.x) :JavaScriptbased, MVC, slower.&lt;/li&gt;&lt;li&gt;Angular (2+) :TypeScriptbased, componentdriven, faster.&lt;/li&gt;&lt;/ul&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;16. What are the main building blocks of Angular?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Modules&lt;/li&gt;&lt;li&gt;Components&lt;/li&gt;&lt;li&gt;Templates&lt;/li&gt;&lt;li&gt;Directives&lt;/li&gt;&lt;li&gt;Services&lt;/li&gt;&lt;li&gt;Pipes&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;17. What is Dependency Injection in Angular?&lt;/h4&gt;&lt;p&gt;DI means Angular creates and injects services instead of you creating them manually. Makes code testable and modular.&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;18. Explain Angular data binding.&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Interpolation : {{ name }}&lt;/li&gt;&lt;li&gt;Property binding : [src]=&quot;imageUrl&quot;&lt;/li&gt;&lt;li&gt;Event binding :(click)=&quot;onClick()&quot;&lt;/li&gt;&lt;li&gt;Twoway binding : [(ngModel)]=&quot;username&quot;&lt;/li&gt;&lt;/ul&gt;check here for more details on &lt;a href=&quot;https://www.instanceofjava.com/2025/02/data-binding-angular.html&quot; rel=&quot;nofollow&quot;&gt;data binding in angular&lt;/a&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;19. What are lifecycle hooks in Angular?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;ngOnInit() : runs after component loads&lt;/li&gt;&lt;li&gt;ngOnChanges() : detects input property changes&lt;/li&gt;&lt;li&gt;ngOnDestroy() :cleanup before component is destroyed&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;for more details on&lt;a href=&quot;https://www.instanceofjava.com/2025/02/angular-life-cycle-hooks.html&quot; target=&quot;_blank&quot;&gt; 8 life cycle hooks angular&amp;nbsp;&lt;/a&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;20. How do Angular components communicate?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;@Input() - Parent - Child&lt;/li&gt;&lt;li&gt;@Output() -Child -Parent&lt;/li&gt;&lt;li&gt;Services with RxJS :Share data across components&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;check this detailed post on&lt;a href=&quot;https://www.instanceofjava.com/2025/02/component-data-sharing-in-angular.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt; 6 different ways of angular communication&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;21. What are Angular directives?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Structural : *ngIf, *ngFor&lt;/li&gt;&lt;li&gt;Attribute : [ngClass], [ngStyle]&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;for detailed &lt;a href=&quot;https://www.instanceofjava.com/2025/02/directives-in-angular.html&quot; target=&quot;_blank&quot;&gt;explanation and examples on angular directives&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;22. What is Angular Routing?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Routing allows navigation between views. Features include:&lt;/li&gt;&lt;li&gt;Route parameters (:id)&lt;/li&gt;&lt;li&gt;Lazy loading&lt;/li&gt;&lt;li&gt;Route guards&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;23. What are Observables in Angular?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Observables (via RxJS) handle async data streams (HTTP requests, events, sockets).&lt;/li&gt;&lt;li&gt;Unlike promises, they can emit multiple values over time.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;24. What is Angular Ivy?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Ivy is the rendering engine that makes Angular apps smaller and faster.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;25. What is Angular Universal?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;It enables ServerSide Rendering (SSR) for SEO and performance.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;26. How does Angular handle forms?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Templatedriven forms : simpler.&lt;/li&gt;&lt;li&gt;Reactive forms:&amp;nbsp; more control, better validation.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;27. What are Angular Pipes?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Transform data in templates. Examples: date, currency, async.&lt;/li&gt;&lt;li&gt;You can also build custom pipes.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;28. What is lazy loading in Angular?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Loads modules only when needed, reducing initial bundle size.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;29. What are Guards in Angular routing?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Guards like CanActivate and CanDeactivate control route access.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;30. What is ViewEncapsulation?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Controls how CSS styles are applied:&lt;/li&gt;&lt;li&gt;Emulated (default)&lt;/li&gt;&lt;li&gt;None&lt;/li&gt;&lt;li&gt;ShadowDOM&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;31. What are standalone components in Angular 17+?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Standalone components don’t need NgModules, making apps simpler.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;32. What are Angular Signals?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Signals are new reactive primitives for managing state, simpler than RxJS in some cases.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;33. What are Angular Zones?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;NgZone tracks async tasks and triggers change detection.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;34. What is AheadofTime (AOT) compilation?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;AOT compiles Angular templates at build time &amp;amp; faster runtime performance.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;35. Difference between templatedriven and reactive forms?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;a href=&quot;https://www.instanceofjava.com/2025/03/angular-template-driven-forms.html&quot; rel=&quot;nofollow&quot;&gt;Template driven&lt;/a&gt;: simple, less code.&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.instanceofjava.com/2025/03/mastering-reactive-forms-in-angular.html&quot;&gt;Reactive&lt;/a&gt;: scalable, great for complex forms.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;36. How do you improve performance in Angular apps?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Lazy loading&lt;/li&gt;&lt;li&gt;OnPush change detection&lt;/li&gt;&lt;li&gt;TrackBy in *ngFor&lt;/li&gt;&lt;li&gt;Caching API responses&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;37. Difference between Subject, BehaviorSubject, and ReplaySubject in RxJS?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Subject:&lt;/b&gt; plain multicast observable.&lt;/li&gt;&lt;li&gt;&lt;b&gt;BehaviorSubject:&lt;/b&gt; holds last value.&lt;/li&gt;&lt;li&gt;&lt;b&gt;ReplaySubject: r&lt;/b&gt;eplays multiple past values.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;38. How do you handle state management in Angular?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;RxJS services&lt;/li&gt;&lt;li&gt;NgRx&lt;/li&gt;&lt;li&gt;Akita&lt;/li&gt;&lt;li&gt;Signals (newer alternative)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;39. Difference between Reactive and Imperative programming?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Reactive: declarative, uses streams (RxJS, signals).&lt;/li&gt;&lt;li&gt;Imperative : stepbystep instructions.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;40. How do you make Angular apps SEOfriendly?&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Angular Universal (SSR)&lt;/li&gt;&lt;li&gt;Prerendering&lt;/li&gt;&lt;li&gt;Meta tags&lt;/li&gt;&lt;li&gt;Lazyloaded images&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Here is Quick Revision for you: 40 Angular &amp;amp; JavaScript Interview Questions&amp;nbsp;&lt;/h3&gt;&lt;div&gt;&lt;b&gt;JavaScript Questions&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Closures: I&lt;/b&gt;nner function remembers variables from outer function even after execution.&lt;/li&gt;&lt;li&gt;&lt;b&gt;var vs let vs const: &lt;/b&gt;var = function scope, let = block scope, const = block scope (no reassignment).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Event delegation:&lt;/b&gt;Attach one listener to parent, handle child events via bubbling.&lt;/li&gt;&lt;li&gt;&lt;b&gt;== vs === : &lt;/b&gt;== loose equality with type coercion, === strict equality.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Event loop :&lt;/b&gt; Handles async tasks by moving them from queue to call stack.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Promises vs async/await:&lt;/b&gt; Promise handles async values; async/await makes them cleaner.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Hoisting:&lt;/b&gt;JS moves declarations (not assignments) to the top at runtime.&lt;/li&gt;&lt;li&gt;&lt;b&gt;null vs undefined:&lt;/b&gt; null = intentional empty, undefined = unassigned.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Arrow functions:&lt;/b&gt; Short syntax, no own this.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Sync vs Async JS: S&lt;/b&gt;ync = one task at a time; Async = tasks in background.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Callback: F&lt;/b&gt;unction passed into another to be executed later.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Prototype: E&lt;/b&gt;nables inheritance in JS objects.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Higherorder function: &lt;/b&gt;Function that takes/returns another function (map, filter).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Deep vs shallow copy: &lt;/b&gt;Shallow copies only one level; deep copies nested too.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Angular interview Questions&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Angular vs AngularJS:&lt;/b&gt; Angular = TypeScript, faster, componentbased; AngularJS = JS, MVC.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Building blocks: M&lt;/b&gt;odules, Components, Templates, Directives, Services, Pipes.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Dependency Injection:&lt;/b&gt; Angular provides services/objects automatically.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Data binding: &lt;/b&gt;Interpolation, property, event, twoway ([(ngModel)]).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Lifecycle hooks: &lt;/b&gt;ngOnInit, ngOnChanges, ngOnDestroy.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Component communication: @&lt;/b&gt;Input, @Output, services with RxJS.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Directives:&lt;/b&gt; Structural (*ngIf, *ngFor) and Attribute (ngClass).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Routing: &lt;/b&gt;Navigation between views, supports lazy loading and guards.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Observables: &lt;/b&gt;Async streams of data (multiple values over time).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Ivy:&lt;/b&gt; New rendering engine, smaller bundles, faster.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Universal (SSR): &lt;/b&gt;Serverside rendering for SEO and performance.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Forms:&lt;/b&gt; Templatedriven (simple) vs Reactive (scalable, powerful).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Pipes:&lt;/b&gt; Transform data (date, currency, async, custom).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Lazy loading:&lt;/b&gt; Loads modules on demand for performance.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Guards:&lt;/b&gt; Control access to routes (CanActivate, CanDeactivate).&lt;/li&gt;&lt;li&gt;&lt;b&gt;ViewEncapsulation: &lt;/b&gt;Controls style scope (Emulated, ShadowDOM, None).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Standalone components :&lt;/b&gt;Angular 17+, no need for NgModules.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Signals : &lt;/b&gt;New reactive state primitive, simpler than RxJS in some cases.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Zones (NgZone) &lt;/b&gt;:Detect async tasks, trigger change detection.&lt;/li&gt;&lt;li&gt;&lt;b&gt;AOT compilation :&lt;/b&gt; Precompiles templates at build time.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Template vs Reactive forms :&lt;/b&gt;Template = simple, Reactive = complex validation.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Performance optimization:&lt;/b&gt; Lazy load, OnPush, TrackBy, cache data.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Subject vs BehaviorSubject vs ReplaySubject :&lt;/b&gt;Subject : plain, Behavior :last value, Replay : past values.&lt;/li&gt;&lt;li&gt;&lt;b&gt;State management:&lt;/b&gt; RxJS, NgRx, Akita, Signals.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Reactive vs Imperative programming:&lt;/b&gt; Reactive = declarative streams, Imperative = stepbystep.&lt;/li&gt;&lt;li&gt;&lt;b&gt;SEO in Angular : A&lt;/b&gt;ngular Universal, prerendering, meta tags, lazyloaded images.&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;please provide your valuable suggestion&#39;s and comments&amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/4797478984794367458/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/08/angular-javascript-interview-questions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/4797478984794367458'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/4797478984794367458'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/08/angular-javascript-interview-questions.html' title=' 40+ Angular and JavaScript Interview Questions'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLZGicKRnQT_QU_Y2Vn4eHYhgIp732vnrvd6J-5R6shD_QjFOnTkfhSZAnGOShD0YWz6LbqMKRpr_oythkTm0_zfrzJ3T6jyqi-Fg_evvtT5boDaD1Vzu23Qkh6Vx9GIon5T7FpgaCUghzYAorpPZQXikuy8Q4lERFGD89T5vFky55oThw3QYoN_XtM5Br/s72-w400-h266-c/angular%20and%20javascript%20interview%20questiosn%20and%20answers.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-6129533116572062362</id><published>2025-03-19T12:41:00.011+05:30</published><updated>2025-10-05T22:09:48.527+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="Javascript interview questions"/><title type='text'>Explain the difference between == and ===</title><content type='html'>&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&amp;nbsp;We all know that == is equality operator is used to compare two values in JavaScript.&lt;/li&gt;&lt;li&gt;=== operator is strict equality operator&lt;/li&gt;&lt;li&gt;Both == &amp;amp; === are used to compare values in JavaScript. then what is the difference between == and === lets discuss in detail&lt;/li&gt;&lt;li&gt;== compares two values by converting them to same data types, 10 == &#39;&#39;10&quot; will be true because string type of 10 converted in to number.&lt;/li&gt;&lt;li&gt;=== compares two values without converting them to same data type. 10 === &quot;10&quot;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.instanceofjava.com/p/javascript-online-compiler.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Use our JavaScript compiler to test your snippets&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Difference between == and === in Javascript&lt;/h3&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1AuuDaCC0xNG_GxOPer3N-wBF8R48OSSKgrD-4M3O0G9wk8aRQf0q3rGh-1RvT3HV3DjqXtrpdzixlfDqGzmsrXBgNaevuUf3XjtUqn7oBzhYgb_hr9dtEJ0E8wrKzrQPnU4pBG5iJ9NJFPM7NEXv_-pcDNtpD9FcxIac4rKHNdw-DB6oKxxyeuRksTUl/s1083/==%20vs%20===%20in%20javascript.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;== vs === in JavaScript&quot; border=&quot;0&quot; data-original-height=&quot;378&quot; data-original-width=&quot;1083&quot; height=&quot;224&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1AuuDaCC0xNG_GxOPer3N-wBF8R48OSSKgrD-4M3O0G9wk8aRQf0q3rGh-1RvT3HV3DjqXtrpdzixlfDqGzmsrXBgNaevuUf3XjtUqn7oBzhYgb_hr9dtEJ0E8wrKzrQPnU4pBG5iJ9NJFPM7NEXv_-pcDNtpD9FcxIac4rKHNdw-DB6oKxxyeuRksTUl/w640-h224/==%20vs%20===%20in%20javascript.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;1.&amp;nbsp; == ( loose Equality)&lt;/b&gt;&lt;/h3&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;p style=&quot;font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; margin: 0px;&quot;&gt;&lt;strong&gt;Checks value equality after type&amp;nbsp;&lt;/strong&gt;&lt;strong&gt;conversion&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; margin: 0px;&quot;&gt;Converts operands to same type before conversion (ex: 10 == &#39;10&#39; here string 10 will be converted in to number)&amp;nbsp;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;2. === Strict equality&lt;/b&gt;&lt;/h3&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;checks value and type without conversion.&lt;/li&gt;&lt;li&gt;return false if types are different.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;table data-end=&quot;426&quot; data-start=&quot;31&quot; node=&quot;[object Object]&quot;&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;Loose ==)&lt;/th&gt;&lt;th&gt;Strict (===)&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;5 vs. &quot;5&lt;/td&gt;&lt;td&gt;true&lt;/td&gt;&lt;td&gt;false&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;0 vs. false&lt;/td&gt;&lt;td&gt;true
  &lt;/td&gt;&lt;td&gt;false&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;null vs. undefined&lt;/td&gt;&lt;td&gt;true&lt;/td&gt;&lt;td&gt;false&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;NaN vs NaN&lt;/td&gt;&lt;td&gt;false&lt;/td&gt;&lt;td&gt;false&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;{} vs {}&lt;/td&gt;&lt;td&gt;false&lt;/td&gt;&lt;td&gt;false&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;Explanation:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;1. 5 == &quot;5&quot; → true(loose)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;- JavaScript converts &quot;5&quot; (string) to 5 (number) before comparison.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; 5 === &quot;5&quot; → false(strict)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;- Different types: number vs. string.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;2. 0 == false → true(loose)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;false is converted to 0, so 0 == 0 is true.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; 0 === false → false(strict)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;- Number vs. boolean, so different types.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;3. null == undefined → true(loose)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;- Special case:null andundefined are only loosely equal to each other.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; null === undefined → false(strict)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;- Different types:null vs.undefined.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;4. NaN == NaN → false(loose)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;NaN is&amp;nbsp; not equal to itselfin JavaScript.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; NaN === NaN → false(strict)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;Same reason here: NaN is never equal to anything, including itself.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;5. {} == {} → false(loose)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;- Objects are compared by **reference**, not by value.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;- Different objects in memory, so alwaysfalse.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; {} === {} → false (strict)&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;- Still different object references.&amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&quot;5&quot; === &quot;5&quot; returns true&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Remember these special cases:&lt;/b&gt;&lt;/h3&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;NaN == NaN &lt;/b&gt;will be &lt;b&gt;false &lt;/b&gt;even with == operator ,Use Number.isNaN(x) to check for NaN.&lt;/li&gt;&lt;li&gt;&lt;b&gt;null == undefined&lt;/b&gt; will be &lt;b&gt;true&lt;/b&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;When to use which operator:&lt;/b&gt;&lt;/h3&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;always use === as it is recommended way and which avoids unexpected type conversion&lt;/li&gt;&lt;li&gt;use == for null/undefined check&lt;/li&gt;&lt;li&gt;if (x == null) { ... } // Catches both `null` and `undefined`.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/6129533116572062362/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/explain-difference-between-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6129533116572062362'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6129533116572062362'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/explain-difference-between-and.html' title='Explain the difference between == and ==='/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1AuuDaCC0xNG_GxOPer3N-wBF8R48OSSKgrD-4M3O0G9wk8aRQf0q3rGh-1RvT3HV3DjqXtrpdzixlfDqGzmsrXBgNaevuUf3XjtUqn7oBzhYgb_hr9dtEJ0E8wrKzrQPnU4pBG5iJ9NJFPM7NEXv_-pcDNtpD9FcxIac4rKHNdw-DB6oKxxyeuRksTUl/s72-w640-h224-c/==%20vs%20===%20in%20javascript.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-3278421392949959376</id><published>2025-03-03T22:04:00.005+05:30</published><updated>2026-02-20T15:12:57.706+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>apex charts angular</title><content type='html'>&lt;p&gt;&lt;strong&gt;A Brief Tutorial on Using ApexCharts with Angular&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJk2p9mgUHKZ9jhl76wWdr9p8y3LFr9ATizHprYYP-ocPyez7KhiHvrc3c9MWTrm-iobKawkNdTg7RB6cu_6RI5uxTAWDCW-_t8Qz8SYvzvmFQyIb_aDKMr2VhojHP3yV4u9L6H9Q6tH4Jz0t3mjV1F4LjlMsfqT06aKUHmPBiHPqM90vyL8h4HA65rcKr/s957/apex%20charts%20angular.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;apex charts angular&quot; border=&quot;0&quot; data-original-height=&quot;552&quot; data-original-width=&quot;957&quot; height=&quot;370&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJk2p9mgUHKZ9jhl76wWdr9p8y3LFr9ATizHprYYP-ocPyez7KhiHvrc3c9MWTrm-iobKawkNdTg7RB6cu_6RI5uxTAWDCW-_t8Qz8SYvzvmFQyIb_aDKMr2VhojHP3yV4u9L6H9Q6tH4Jz0t3mjV1F4LjlMsfqT06aKUHmPBiHPqM90vyL8h4HA65rcKr/w640-h370/apex%20charts%20angular.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Quick Installation&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Install dependencies:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npm i apexcharts ng-apexcharts
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Import Module&lt;/strong&gt; (app.module.ts)&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;&lt;strong&gt;Basic Line Chart Example&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Component Template&lt;/strong&gt; (app.component.html)&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;apx-chart 
  [series]=&quot;chartOptions.series&quot; 
  [chart]=&quot;chartOptions.chart&quot;
  [xaxis]=&quot;chartOptions.xaxis&quot; 
  [title]=&quot;chartOptions.title&quot;&amp;gt;
&amp;lt;/apx-chart&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Component Class&lt;/strong&gt; (app.component.ts)&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Component } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;
})
export class AppComponent {
  chartOptions = {
    series: [{ data: [10, 20, 30, 40] }],
    chart: { type: &#39;line&#39; },
    xaxis: { categories: [&#39;Jan&#39;, &#39;Feb&#39;, &#39;Mar&#39;, &#39;Apr&#39;] },
    title: { text: &#39;Basic Line Chart&#39; }
  };
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Key Features&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dynamic Updates&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;updateChart() {
  const newData = this.chartOptions.series[0].data.map(() =&amp;gt; 
    Math.floor(Math.random() * 100)
  );
  this.chartOptions = { ...this.chartOptions, series: [{ data: newData }] };
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Mixed Chart Types&lt;/strong&gt; – Ability to use multiple chart types in one visualization.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;&lt;strong&gt;Example Links&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Basic Demo: Pie Chart&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;chartOptions = {
  series: [44, 55, 13],
  chart: { type: &#39;pie&#39;, width: 380 },
  labels: [&#39;Team A&#39;, &#39;Team B&#39;, &#39;Team C&#39;]
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Common Configurations&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Styling&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;chartOptions = {
  chart: { foreColor: &#39;#333&#39; }, // Text color
  colors: [&#39;#4CAF50&#39;, &#39;#E91E63&#39;], // Data colors
  grid: { borderColor: &#39;#e0e0e0&#39; } // Grid lines
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Tooltip Customization&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;tooltip: {
  theme: &#39;dark&#39;,
  x: { show: false }, // Hide x-axis tooltip
  y: { formatter: (val: number) =&amp;gt; `${val} users` } // Custom label
}
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Tips for Troubleshooting&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Chart Not Rendering?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Make sure NgApexchartsModule is imported.&lt;/li&gt;
&lt;li&gt;Ensure there are no typos with option properties (e.g., xaxis vs xAxis).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Not Updating?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Assign the entire chartOptions object to enable Angular change detection.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Performance Issues?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use ChangeDetectionStrategy.OnPush for optimization.&lt;/li&gt;
&lt;li&gt;Debounce quick updates usingrxjs/debounceTime
  .&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Why ApexCharts?&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Free &amp;amp; Open Source&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;MIT Licensed&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;Advanced Features&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Chart Annotations&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Labels&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Brush Charts&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;Native Angular Support&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Ready-to-use code snippets&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;
&lt;/ul&gt;
&lt;p&gt;For more advanced features like annotations, data labels, brush charts, and others, check out the &lt;a href=&quot;https://apexcharts.com/docs/&quot;&gt;ApexCharts Documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/3278421392949959376/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/apexcharts-with-angular.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/3278421392949959376'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/3278421392949959376'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/apexcharts-with-angular.html' title='apex charts angular'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJk2p9mgUHKZ9jhl76wWdr9p8y3LFr9ATizHprYYP-ocPyez7KhiHvrc3c9MWTrm-iobKawkNdTg7RB6cu_6RI5uxTAWDCW-_t8Qz8SYvzvmFQyIb_aDKMr2VhojHP3yV4u9L6H9Q6tH4Jz0t3mjV1F4LjlMsfqT06aKUHmPBiHPqM90vyL8h4HA65rcKr/s72-w640-h370-c/apex%20charts%20angular.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-6773320032934254859</id><published>2025-03-03T19:02:00.002+05:30</published><updated>2025-03-11T21:38:56.245+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>var, let, and const in JavaScript</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;Decrypt var, let, and const in JavaScript: A Complete Analysis&lt;/strong&gt;&lt;/div&gt;
&lt;p&gt;A JavaScript variable is typically created using one of three keywords: var, let, or const. Each behaves differently in terms of &lt;strong&gt;scope&lt;/strong&gt;, &lt;strong&gt;hoisting&lt;/strong&gt;, and &lt;strong&gt;mutability&lt;/strong&gt;, though they all might function very similarly. This article breaks down the differences between these keywords in simple terms so that you can write cleaner and more secure code&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtJWgWzsMiK7IXKerG8dJ5wdvAxDX-Z3K3ToixSmwq6Vm2aKAfWcAGJT46l-1bc8l9Gn8nRZ-7TRUCamykumvLUHIpEamrrSd4DoM22kNAQZC7FjDHVSJ9Kt0hXIro3M4yazU6fifZpq6BklMnBoucA1EIrPM8EzpoE6KNEnbO94G5saV8v23U7lX6viiH/s755/var%20let%20const%20in%20javascript.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;var let const in javascript&quot; border=&quot;0&quot; data-original-height=&quot;755&quot; data-original-width=&quot;745&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtJWgWzsMiK7IXKerG8dJ5wdvAxDX-Z3K3ToixSmwq6Vm2aKAfWcAGJT46l-1bc8l9Gn8nRZ-7TRUCamykumvLUHIpEamrrSd4DoM22kNAQZC7FjDHVSJ9Kt0hXIro3M4yazU6fifZpq6BklMnBoucA1EIrPM8EzpoE6KNEnbO94G5saV8v23U7lX6viiH/w632-h640/var%20let%20const%20in%20javascript.png&quot; width=&quot;632&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h2&gt;&lt;strong&gt;1. var: The Old Legacy Variable Declaration&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Scope: Function Scope&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Variables declared with var are &lt;strong&gt;function scoped&lt;/strong&gt; (or globally scoped if defined outside a function).&lt;/li&gt;
&lt;li&gt;They&#39;re accessible from anywhere within the function where they are defined.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;function exampleVar() {
 if (true) {
  var x = 10;
 }
 console.log(x); // 10 (not limited to the block)
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Hoisting&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;var declarations are &lt;strong&gt;hoisted&lt;/strong&gt; to the top of the containing function and initialized with undefined.&lt;/li&gt;
&lt;li&gt;This can lead to unexpected behavior if not taken into account.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log(y); // undefined (no error)
var y = 5;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Redeclaration and Reassignment&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;var allows redeclaration and reassignment:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;var z = 1;
var z = 2; // No error
z = 3;     // Reassignment allowed
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Drawbacks&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No block scoping&lt;/strong&gt;: Variables leak into blocks like loops or conditionals.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prone to bugs&lt;/strong&gt;: May cause unintended side effects from hoisting and redeclaration.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;2. let: The Modern Block-Scoped Variable&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Block-Level Scope&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;let variables are &lt;strong&gt;block scoped&lt;/strong&gt;: they only exist within {} like loops, conditionals, or function bodies.&lt;/li&gt;
&lt;li&gt;Safer and more predictable than var.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;function exampleLet() {
 if (true) {
  let a = 20;
  console.log(a); // 20
 }
 console.log(a); // ReferenceError: a is not defined
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Hoisting and the Temporal Dead Zone (TDZ)&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;let declarations are hoisted but not initialized.&lt;/li&gt;
&lt;li&gt;Accessing them before declaration results in ReferenceError.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log(b); // ReferenceError: Cannot access &#39;b&#39; before initialization
let b = 5;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Reassignment and Redeclaration&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;let allows reassignment but not redeclaration.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let c = 10;
c = 20; // Allowed
let c = 30; // SyntaxError: Identifier &#39;c&#39; has already been declared
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;3. const: Constants for Immutable Values&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Scope: Like let, Block Scoped&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Variables created with const are block scoped, meaning they can only be accessed within their defining block.&lt;/li&gt;
&lt;li&gt;They cannot be &lt;strong&gt;reassigned&lt;/strong&gt; after initialization.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const PI = 3.14;
PI = 3.14159; // TypeError: Assignment to constant variable
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Immutable ≠ Unchangeable Values&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;With const, you cannot &lt;strong&gt;reassign&lt;/strong&gt; values, but you can mutate them.&lt;/li&gt;
&lt;li&gt;Objects and arrays declared with const can have their properties/methods altered.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const person = { name: &quot;Alice&quot; };
person.name = &quot;Bob&quot;; // Valid
person = { name: &quot;Charlie&quot; }; // TypeError (reassignment not allowed)
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Hoisting and the Temporal Dead Zone (TDZ)&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Similar to let, const declarations are also hoisted and trigger the TDZ.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;Key Differences Summary&lt;/strong&gt;&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;var&lt;/th&gt;
&lt;th&gt;let&lt;/th&gt;
&lt;th&gt;const&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Scope&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Function/Global&lt;/td&gt;
&lt;td&gt;Block&lt;/td&gt;
&lt;td&gt;Block&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Hoisting&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes (initialized)&lt;/td&gt;
&lt;td&gt;Yes (not initialized)&lt;/td&gt;
&lt;td&gt;Yes (not initialized)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Redeclaration&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Not allowed&lt;/td&gt;
&lt;td&gt;Not allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Reassignment&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Not allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;TDZ&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;&lt;strong&gt;When to Use Each&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;const&lt;/strong&gt; (Recommended):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For values that shouldn’t be reassigned (e.g., configuration objects, API keys).&lt;/li&gt;
&lt;li&gt;Used by default unless reassignment is needed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;let&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When a variable requires &lt;strong&gt;reassignment&lt;/strong&gt; (e.g., loop counters, state changes).&lt;/li&gt;
&lt;li&gt;More robust than var with block scope.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Rarely needed in modern JavaScript. Use only in legacy projects or specific cases.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Slipping into Global Variable Declarations&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;function badPractice() {
 for (var i = 0; i &amp;lt; 5; i++) { /*... */ }
 console.log(i); // 5 (leaked outside loop)
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Fix&lt;/strong&gt;: Use let for block scoping.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Issues Related to TDZ&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let greeting = &quot;Hello&quot;;
if (true) {
 console.log(greeting); // ReferenceError (TDZ)
 let greeting = &quot;Hi&quot;;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Fix&lt;/strong&gt;: Define variables at the top of their scopes.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Misunderstandings of const’s Immutability&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const arr = [1, 2];
arr.push(3); // Valid (array contents changed)
arr = [4, 5]; // TypeError (reassignment not allowed)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Best Practices&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Use const by default&lt;/strong&gt;, and let only when reassignment is necessary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoid var &lt;/strong&gt;, except for maintaining legacy code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Declare variables at the top&lt;/strong&gt; to avoid TDZ errors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use linters like ESLint&lt;/strong&gt; to enforce best practices.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Understanding var, let, and const is crucial for writing secure JavaScript. Embracing block scope (let&lt;/p&gt;/const) and immutability (const) reduces bugs and makes your code easier to debug and maintain.
&lt;p&gt;Key takeaways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;const prevents reassignment but not mutation.&lt;/li&gt;
&lt;li&gt;let and const are the modern standard in JavaScript.&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/6773320032934254859/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/var-let-and-const-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6773320032934254859'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6773320032934254859'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/var-let-and-const-in-javascript.html' title='var, let, and const in JavaScript'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtJWgWzsMiK7IXKerG8dJ5wdvAxDX-Z3K3ToixSmwq6Vm2aKAfWcAGJT46l-1bc8l9Gn8nRZ-7TRUCamykumvLUHIpEamrrSd4DoM22kNAQZC7FjDHVSJ9Kt0hXIro3M4yazU6fifZpq6BklMnBoucA1EIrPM8EzpoE6KNEnbO94G5saV8v23U7lX6viiH/s72-w632-h640-c/var%20let%20const%20in%20javascript.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-6633824086888218205</id><published>2025-03-03T17:07:00.014+05:30</published><updated>2025-03-19T09:41:45.815+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>Angular chart library Highcharts </title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Highcharts with Angular&lt;/b&gt;&lt;/div&gt;
&lt;p&gt;Highcharts is a powerful JavaScript library for creating interactive charts and graphs. When combined with Angular, a robust framework for building web applications, you can create dynamic, data-driven visualizations with ease. This guide will walk you through integrating Highcharts into an Angular project, from setup to advanced configurations.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7uKCpU7nAtlXsP4EORFIfBwBrxqnA-PRUHCa94XyFCAwNevQFsFoT-MD-htgXEm8GQ9p_bxWcV5JsZXq3O8EElIS-8p1Ams1WAXrummuS-2mN8w6E81LqaRR0wYgEGSbmO3neMwqg02gv08yAOTN-mb9DEGPFKmgeJ8ZNB3wlkMlOP5-KrC1u4wD0TkBZ/s1102/highcharts%20angular.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;highcharts angular&quot; border=&quot;0&quot; data-original-height=&quot;596&quot; data-original-width=&quot;1102&quot; height=&quot;346&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7uKCpU7nAtlXsP4EORFIfBwBrxqnA-PRUHCa94XyFCAwNevQFsFoT-MD-htgXEm8GQ9p_bxWcV5JsZXq3O8EElIS-8p1Ams1WAXrummuS-2mN8w6E81LqaRR0wYgEGSbmO3neMwqg02gv08yAOTN-mb9DEGPFKmgeJ8ZNB3wlkMlOP5-KrC1u4wD0TkBZ/w640-h346/highcharts%20angular.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h2&gt;Why Highcharts with Angular?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Rich Features&lt;/strong&gt;: Supports line, bar, pie, scatter, and complex charts such as heatmaps.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interactivity&lt;/strong&gt;: Zooming in and out, tooltips, and dynamic updates enhance user engagement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Customization&lt;/strong&gt;: Theming, annotations, and responsive design.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Angular Compatibility&lt;/strong&gt;: The official highcharts-angular wrapper simplifies integration. Say goodbye to expensive custom solutions!&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Prerequisites&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Node.js and npm tools installed.&lt;/li&gt;
&lt;li&gt;Angular CLI:
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npm install -g @angular/cli
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Basic knowledge of Angular components and modules.&lt;/li&gt;
&lt;/ul&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAJKlSsjuNAM1v0rl_oGWMFT0QhMduAyqhk3joXSWDo3uRTQTGdpHPeNmRHOaQI1S41iKBGZ-v2CPEpcueZ_irIDuJt1wEX8mj9k1J0yRhBknlpClS4Z5Z558f8XZrSDO8-NOP1YWQoEB06APU2W-y0T2qyW-KHN1eN8DMpnZz2d2H72eQSNevJgtpuAda/s717/high%20charts%20angular.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;high charts angular&quot; border=&quot;0&quot; data-original-height=&quot;390&quot; data-original-width=&quot;717&quot; height=&quot;348&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAJKlSsjuNAM1v0rl_oGWMFT0QhMduAyqhk3joXSWDo3uRTQTGdpHPeNmRHOaQI1S41iKBGZ-v2CPEpcueZ_irIDuJt1wEX8mj9k1J0yRhBknlpClS4Z5Z558f8XZrSDO8-NOP1YWQoEB06APU2W-y0T2qyW-KHN1eN8DMpnZz2d2H72eQSNevJgtpuAda/w640-h348/high%20charts%20angular.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;b&gt;Project Setup&lt;/b&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Create an Angular Project&lt;/strong&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ng new highcharts-angular-demo
cd highcharts-angular-demo
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Install Dependencies&lt;/strong&gt;
Install Highcharts and Angular wrappers:
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npm install highcharts highcharts-angular
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Basic Integration&lt;/h2&gt;
&lt;h3&gt;1. Import HighchartsModule&lt;/h3&gt;
&lt;p&gt;Add HighchartsModule to app.module.ts:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { NgModule } from &#39;@angular/core&#39;;
import { BrowserModule } from &#39;@angular/platform-browser&#39;;
import { AppComponent } from &#39;./app.component&#39;;
import { HighchartsChartModule } from &#39;highcharts-angular&#39;;

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, HighchartsChartModule],
  bootstrap: [AppComponent]
})
export class AppModule {}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2. Create a Chart Component&lt;/h3&gt;
&lt;p&gt;Inapp.component.ts, define chart options:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Component } from &#39;@angular/core&#39;;
import * as Highcharts from &#39;highcharts&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;
})
export class AppComponent {
  Highcharts: typeof Highcharts = Highcharts;
  chartOptions: Highcharts.Options = {
    title: { text: &#39;Monthly Sales&#39; },
    series: [{ type: &#39;line&#39;, data: [1, 2, 3, 4, 5] }]
  };
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In app.component.html, render the chart:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;highcharts-chart
  [Highcharts]=&quot;Highcharts&quot;
  [options]=&quot;chartOptions&quot;
  style=&quot;width: 100%; height: 400px; display: block;&quot;&amp;gt;
&amp;lt;/highcharts-chart&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;3. Run the App&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ng serve
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Visit http://localhost:4200 and see your chart!&lt;/p&gt;
&lt;h2&gt;Dynamic Charts&lt;/h2&gt;
&lt;p&gt;Update charts reactively using Angular data binding.&lt;/p&gt;
&lt;h3&gt;1. Add a Button to Update Data&lt;/h3&gt;
&lt;p&gt;In app.component.html:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;button (click)=&quot;updateData()&quot;&amp;gt;Add Data&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2. Implement Data Update Logic&lt;/h3&gt;
&lt;p&gt;In app.component.ts:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;export class AppComponent {
  //...existing code...
  updateData() {
    this.chartOptions = {
      ...this.chartOptions,
      series: [{ type: &#39;line&#39;, data: [...this.chartOptions.series[0].data, Math.random() * 10] }]
    };
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Configuration &amp;amp; Customization&lt;/h2&gt;
&lt;h3&gt;1. Chart Types&lt;/h3&gt;
&lt;p&gt;Change type in series to &#39;bar&#39;, &#39;pie&#39;, etc.:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;series: [{ type: &#39;bar&#39;, data: [10, 20, 30] }]
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2. Styling&lt;/h3&gt;
&lt;p&gt;Customize colors, axes, and tooltips:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;chartOptions: Highcharts.Options = {
  chart: { backgroundColor: &#39;#f4f4f4&#39; },
  xAxis: { title: { text: &#39;Month&#39; } },
  yAxis: { title: { text: &#39;Sales&#39; } },
  tooltip: { valueSuffix: &#39; units&#39; }
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;3. Exporting&lt;/h3&gt;
&lt;p&gt;Enable exporting to PNG/PDF:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;exporting: { enabled: true }
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Advanced Features&lt;/h2&gt;
&lt;h3&gt;1. Highcharts Modules&lt;/h3&gt;
&lt;p&gt;Import additional features like 3D charts:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npm install highcharts-3d
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In app.component.ts:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import HC_3D from &#39;highcharts/highcharts-3d&#39;;
HC_3D(Highcharts);
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2. Lazy Loading Chart&lt;/h3&gt;
&lt;p&gt;Load charts on demand using Angular&#39;s &lt;code inline=&quot;&quot;&gt;ngOnInit&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;async ngOnInit() {
  const Highcharts = await import(&#39;highcharts&#39;);
  // Initialize chart here
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;3. Performance Optimization&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Use OnPush change detection.&lt;/li&gt;
&lt;li&gt;Debounce rapid data updates.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Common Issues &amp;amp; Solutions&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Chart Not Updating&lt;/strong&gt;: Reassign chartOptions instead of mutating it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing Dependencies&lt;/strong&gt;: Ensure highcharts andhighcharts-angular versions match.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Resize Issues&lt;/strong&gt;: CallHighcharts.charts[0].reflow() on window resize.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;Highcharts and Angular together offer a powerful toolkit for data visualization. This guide has enabled you to set up, customize, and optimize charts in an Angular app. Explore the &lt;a href=&quot;https://www.highcharts.com/docs&quot;&gt;Highcharts documentation&lt;/a&gt; for more advanced features such as stock charts or accessibility options.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/6633824086888218205/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-chart-library-highcharts.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6633824086888218205'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6633824086888218205'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-chart-library-highcharts.html' title='Angular chart library Highcharts '/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7uKCpU7nAtlXsP4EORFIfBwBrxqnA-PRUHCa94XyFCAwNevQFsFoT-MD-htgXEm8GQ9p_bxWcV5JsZXq3O8EElIS-8p1Ams1WAXrummuS-2mN8w6E81LqaRR0wYgEGSbmO3neMwqg02gv08yAOTN-mb9DEGPFKmgeJ8ZNB3wlkMlOP5-KrC1u4wD0TkBZ/s72-w640-h346-c/highcharts%20angular.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-8928981500852774707</id><published>2025-03-02T18:36:00.002+05:30</published><updated>2025-10-05T22:10:54.056+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>how to merge objects in javascript</title><content type='html'>&lt;p&gt;There are various ways to combine two objects in JavaScript. Whether you want to conduct a &lt;strong&gt;shallow&lt;/strong&gt; or &lt;strong&gt;deep&lt;/strong&gt; merge will determine which technique you use.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmtTSPnJUOUW-XPN-sWeLpU-QslQnkiv506udeIKM8kcPs-gm8bClUDOIXiVbRXRYx_bJZIerFyH_PGrMGqU1peSk9a6PYXRc0qm_wLjZRW5p3LsXsbFR_W5wg8AVuUa-p3SHN-gt7RyVMV2qZpVyctIPB2CYbVGx3oK3XVs20x4vDdBLht92L_yGQlbC_/s723/merge%20objects%20in%20javascript.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;merge objects in javascript&quot; border=&quot;0&quot; data-original-height=&quot;543&quot; data-original-width=&quot;723&quot; height=&quot;480&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmtTSPnJUOUW-XPN-sWeLpU-QslQnkiv506udeIKM8kcPs-gm8bClUDOIXiVbRXRYx_bJZIerFyH_PGrMGqU1peSk9a6PYXRc0qm_wLjZRW5p3LsXsbFR_W5wg8AVuUa-p3SHN-gt7RyVMV2qZpVyctIPB2CYbVGx3oK3XVs20x4vDdBLht92L_yGQlbC_/w640-h480/merge%20objects%20in%20javascript.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h3&gt;&lt;strong&gt;1. Shallow Merge (Overwrite Properties) in Javascript&lt;/strong&gt;&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Using Object.assign()&lt;/strong&gt;:&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;const obj1 = { a: 1, b: 2 };&lt;/p&gt;&lt;p&gt;const obj2 = { b: 3, c: 4 };&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;const merged = Object.assign({}, obj1, obj2);&lt;/p&gt;&lt;p&gt;// Result: { a: 1, b: 3, c: 4 }&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Using the Spread Operator (...)&lt;/strong&gt;:&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;const merged = {...obj1,...obj2 };&lt;/p&gt;&lt;p&gt;// Result: { a: 1, b: 3, c: 4 }&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Both methods are &lt;strong&gt;shallow merges&lt;/strong&gt;—nested objects/arrays are all &lt;strong&gt;by reference&lt;/strong&gt; (not cloned). If there are duplicate keys, subsequent properties will overwrite prior ones.&lt;/p&gt;&lt;h3&gt;&lt;strong&gt;2. Deep Merge (Recursively Combine Nested Properties) in javascript&lt;/strong&gt;&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Using Lodash&lt;/strong&gt;:&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;const merged = _.merge({}, obj1, obj2);&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Custom Deep Merge Function&lt;/strong&gt; (Simplified Example):&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;function deepMerge(target, source) {&lt;/p&gt;&lt;p&gt;for (const key in source) {&lt;/p&gt;&lt;p&gt;if (source[key] instanceof Object &amp;amp;&amp;amp; target[key]) {&lt;/p&gt;&lt;p&gt;deepMerge(target[key], source[key]);&lt;/p&gt;&lt;p&gt;} else {&lt;/p&gt;&lt;p&gt;target[key] = source[key];&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;return target;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;const merged = deepMerge({}, { a: { x: 1 } }, { a: { y: 2 } });&lt;/p&gt;&lt;p&gt;// Result: { a: { x: 1, y: 2 } }&lt;/p&gt;&lt;h3&gt;&lt;strong&gt;Other Considerations about merging objects in javascript&lt;/strong&gt;&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Shallow Merge&lt;/strong&gt;: For simple scenarios, employ Object.assign() or the spread operator.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Deep Merge&lt;/strong&gt;: For greater resilience, use a tool such as Lodash (e.g. _.merge()), which is able to contend with sophisticated structures, including arrays and null values&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Overwriting Behavior&lt;/strong&gt;: In situations involving conflict over keys, later objects always &lt;strong&gt;win&lt;/strong&gt;.&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/8928981500852774707/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/how-to-merge-objects-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/8928981500852774707'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/8928981500852774707'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/how-to-merge-objects-in-javascript.html' title='how to merge objects in javascript'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmtTSPnJUOUW-XPN-sWeLpU-QslQnkiv506udeIKM8kcPs-gm8bClUDOIXiVbRXRYx_bJZIerFyH_PGrMGqU1peSk9a6PYXRc0qm_wLjZRW5p3LsXsbFR_W5wg8AVuUa-p3SHN-gt7RyVMV2qZpVyctIPB2CYbVGx3oK3XVs20x4vDdBLht92L_yGQlbC_/s72-w640-h480-c/merge%20objects%20in%20javascript.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-1593376332759771008</id><published>2025-03-02T18:20:00.004+05:30</published><updated>2025-03-02T18:24:56.004+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>Preventdefault vs stoppropagation javascript</title><content type='html'>&lt;p&gt;&amp;nbsp;Here is a brand new essay about &lt;strong&gt;preventDefault()&lt;/strong&gt; versus &lt;strong&gt;stopPropagation()&lt;/strong&gt; in &lt;strong&gt;Angular&lt;/strong&gt;. I&#39;ll start by comparing how they&#39;re used with &lt;strong&gt;preventDefault() and stopPropagation&lt;/strong&gt;. We&#39;ll also offer a case study for each method and share some tips.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIYJ8QS5Po3ica-AzJlDFDle3N2vHsLD6_4XX8u7Ua7eAxuKB3o98xE8EZAgD7TnwGlsrNttkaY8xZPsv27qQ7ubc9K537a5lx7CcpSxVosxYoPCTX23gTYv0n3l9k9kCnkwYOCIRvtU-bq6IEN4-tbBh-olbz1ru51_0uggwqIROgO-TAUCOAXQT6_GQ7/s738/preventdefault%20vs%20stoppropagation.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;preventdefault vs stoppropagation javascript angular&quot; border=&quot;0&quot; data-original-height=&quot;528&quot; data-original-width=&quot;738&quot; height=&quot;458&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIYJ8QS5Po3ica-AzJlDFDle3N2vHsLD6_4XX8u7Ua7eAxuKB3o98xE8EZAgD7TnwGlsrNttkaY8xZPsv27qQ7ubc9K537a5lx7CcpSxVosxYoPCTX23gTYv0n3l9k9kCnkwYOCIRvtU-bq6IEN4-tbBh-olbz1ru51_0uggwqIROgO-TAUCOAXQT6_GQ7/w640-h458/preventdefault%20vs%20stoppropagation.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;1. event.preventDefault() in Angular&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: To prevent the default action of a browser event (for example, a submission form or hyperlink).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use in Angular&lt;/strong&gt;: With Angular’s &lt;strong&gt;event binding syntax&lt;/strong&gt; (event)=&quot;handler($event)&quot;&lt;/li&gt;&lt;/ul&gt;.

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: Prevent link from navigating:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;a href=&quot;https://dangerous.com&quot; (click)=&quot;onLinkClick($event)&quot;&amp;gt;Dangerous Link&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;onLinkClick(event: MouseEvent) {
  event.preventDefault(); // Stop navigation
  // Custom logic here (e.g., show a modal instead)
}
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Scenarios Commonly Encountered in Angular&lt;/strong&gt;:
&lt;ul&gt;
&lt;li&gt;When using (submit) and &amp;lt;form&amp;gt; to cancel form submission.&lt;/li&gt;
&lt;li&gt;Shut off the default behaviors of anchor tags, buttons, or form fields.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h3&gt;&lt;strong&gt;2. event.stopPropagation() in Angular&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Stop event bubbling/capturing in the DOM tree.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use in Angular&lt;/strong&gt;: To prevent parent and descendant components from receiving the exact same events.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: Don&#39;t let a press on a button cause the parent &amp;lt;div&amp;gt; method to fire:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;div (click)=&quot;onParentClick()&quot;&amp;gt;
  &amp;lt;button (click)=&quot;onButtonClick($event)&quot;&amp;gt;Click Me&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;onButtonClick(event: MouseEvent) {
  event.stopPropagation(); // The mom&#39;s onParentClick() will not fire
  // Take care of button click
}
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Specific Cases&lt;/strong&gt;:
&lt;ul&gt;
&lt;li&gt;Stop nested components from propagating events.&lt;/li&gt;
&lt;li&gt;Avoid conflict with other handlers dealing in similar space (e.g., dropdown, modals).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h3&gt;&lt;strong&gt;3. Key Differences in Angular&lt;/strong&gt;&lt;/h3&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;preventDefault()&lt;/th&gt;
&lt;th&gt;stopPropagation()&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Focus&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Prevent browser from default behavior (e.g., form submit)&lt;/td&gt;
&lt;td&gt;Prevent DOM event bubbling/capturing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Use Case in Angular&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Cancel navigation, form submission&lt;/td&gt;
&lt;td&gt;Parent and child Components surface events in their own spaces&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Template Syntax&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;(submit)=&quot;onSubmit($event)&quot;&lt;/td&gt;
&lt;td&gt;(click)=&quot;onClick($event)&quot;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;hr /&gt;
&lt;h3&gt;&lt;strong&gt;4. Using Both Together in Angular&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Example: Handle a custom form action without submitting &lt;strong&gt;and&lt;/strong&gt; prevent parent components from interpreting it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;form (submit)=&quot;onFormSubmit($event)&quot;&amp;gt;
  &amp;lt;button type=&quot;submit&quot;&amp;gt;Save&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;onFormSubmit(event: Event) {
  event.preventDefault(); // Halt the default form submission
  event.stopPropagation(); // Don&#39;t let parent events hear about this
  this.saveData(); // Custom logic here, e.g. http call
}
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h3&gt;&lt;strong&gt;5. Angular Special Notes&lt;/strong&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;strong&gt;Event Modifiers&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Angular &lt;strong&gt;does not&lt;/strong&gt; provide its own event modifiers like Vue&#39;s .prevent or .stop. You need to call the method explicitly instead:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;form (submit)=&quot;onSubmit($event)&quot;&amp;gt;&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;onSubmit(event: Event) {
  event.preventDefault(); // Manually call
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;&lt;strong&gt;Component Communication&lt;/strong&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;stopPropagation() only affects &lt;strong&gt;DOM events&lt;/strong&gt;, not Angular&#39;s @Output().&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;app-child (customEvent)=&quot;onChildEvent()&quot;&amp;gt;&amp;lt;/app-child&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// ChildComponent
@Output() customEvent = new EventEmitter();
emitEvent() {
  this.customEvent.emit();
  // Whether the parent code listens to DOM event propagation, onChildEvent() will fire
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;&lt;strong&gt;Change Detection&lt;/strong&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Neither method affects Angular change detection.&lt;/li&gt;
&lt;li&gt;Use NgZone or ChangeDetectorRef when events bypass Angular&#39;s domain (for example, setTimeout).&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h3&gt;&lt;strong&gt;6. Angular Best Practices&lt;/strong&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;How to Break Free from Inline Calls&lt;/strong&gt;:
Place your methods right in the constituent, rather than inside the official template:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;a href=&quot;#&quot; (click)=&quot;onClick($event)&quot;&amp;gt;Link&amp;lt;/a&amp;gt;
&amp;lt;button (click)=&quot;onClick($event)&quot;&amp;gt;Link&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;For Component Communication, Use EventEmitter &lt;/strong&gt;:
Old-fashioned emit rather than DOM event. Use @Output() Stateless Copy for State Changes: Ensure data is never changed after a call to preventDefault(), to be sure change detection fires.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;hr /&gt;
&lt;h3&gt;&lt;strong&gt;7. Common Traps&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lack of $event&lt;/strong&gt;: Don’t forget to mention $event in the template:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;button (click)=&quot;onClick&quot;&amp;gt;Click&amp;lt;/button&amp;gt; ❌ Incorrect
&amp;lt;button (click)=&quot;onClick($event)&quot;&amp;gt;Click&amp;lt;/button&amp;gt; ✅ Correct
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Too Much stopPropagation()&lt;/strong&gt;: For highly intricate component trees, it can only create debugging headaches.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;In Angular:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;preventDefault()&lt;/strong&gt;: Regulate browser defaults (e.g., forms, links).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;stopPropagation()&lt;/strong&gt;: Control event flow between some connected DOM elements/components.&lt;/li&gt;
&lt;li&gt;Use both in tandem to finely manage your event firing and your UI experience.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/1593376332759771008/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/preventdefault-vs-stoppropagation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/1593376332759771008'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/1593376332759771008'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/preventdefault-vs-stoppropagation.html' title='Preventdefault vs stoppropagation javascript'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIYJ8QS5Po3ica-AzJlDFDle3N2vHsLD6_4XX8u7Ua7eAxuKB3o98xE8EZAgD7TnwGlsrNttkaY8xZPsv27qQ7ubc9K537a5lx7CcpSxVosxYoPCTX23gTYv0n3l9k9kCnkwYOCIRvtU-bq6IEN4-tbBh-olbz1ru51_0uggwqIROgO-TAUCOAXQT6_GQ7/s72-w640-h458-c/preventdefault%20vs%20stoppropagation.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-7607065267788025490</id><published>2025-03-02T18:05:00.007+05:30</published><updated>2025-03-02T18:10:29.361+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="Javascript interview questions"/><title type='text'>splice vs slice in javascript</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;strong&gt;&#39;splice()&#39; and &#39;slice()&#39; are two array techniques in JavaScript that are often confused with each other.&lt;/strong&gt; In order to resolve these ambiguities, we will compare the two methods below step by step.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiUGC1DgEKmSSAnDdxuqbp3zRg1THd4Y8g0rOznhnKjbBr9TiOSzVHPd2RQvTstwMk3akr8b75bpfOnKcy9z8J9cLLeDDBaCOESN8n5jGuhgr4YXaW8VvrN6uPazWDuJg2ZdQ7te1BfaS8XSOoVRGpX27ypMNQ_Ma_5Y9w8uT2gXKJGmE6WztHw-epJqkU/s873/splice%20vs%20slice%20javascript.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;splice vs slice javascript&quot; border=&quot;0&quot; data-original-height=&quot;671&quot; data-original-width=&quot;873&quot; height=&quot;492&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiUGC1DgEKmSSAnDdxuqbp3zRg1THd4Y8g0rOznhnKjbBr9TiOSzVHPd2RQvTstwMk3akr8b75bpfOnKcy9z8J9cLLeDDBaCOESN8n5jGuhgr4YXaW8VvrN6uPazWDuJg2ZdQ7te1BfaS8XSOoVRGpX27ypMNQ_Ma_5Y9w8uT2gXKJGmE6WztHw-epJqkU/w640-h492/splice%20vs%20slice%20javascript.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h3&gt;&lt;strong&gt;1. Creating a Slice: The Simple Version&lt;/strong&gt;&lt;/h3&gt;&lt;h4&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/h4&gt;&lt;p&gt;Evaluates only a small part of an array without changing it.&lt;/p&gt;&lt;h4&gt;&lt;strong&gt;Usage&lt;/strong&gt;&lt;/h4&gt;&lt;p&gt;array.slice(startIndex, endIndex);&lt;/p&gt;&lt;h4&gt;&lt;strong&gt;Main Points&lt;/strong&gt;&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;Produces a &lt;strong&gt;new array&lt;/strong&gt; consisting of the elements from startIndex to endIndex (not inclusive).&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Changes made to the original array are not visible.&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;If indexes are negative, they count from the end.&lt;/li&gt;&lt;li&gt;If no arguments are given, then array.slice() makes a &lt;strong&gt;shallow copy&lt;/strong&gt; of the entire array.&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/h4&gt;&lt;p&gt;const fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;];&lt;/p&gt;&lt;p&gt;const sliced = fruits.slice(1, 3);&lt;/p&gt;&lt;p&gt;console.log(sliced); // [&#39;banana&#39;, &#39;cherry&#39;]&lt;/p&gt;&lt;p&gt;console.log(fruits); // [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;] (that&#39;s how it remained)&lt;/p&gt;&lt;h3&gt;&lt;strong&gt;2. splice(): Add/Remove Elements from an Array&lt;/strong&gt;&lt;/h3&gt;&lt;h4&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/h4&gt;&lt;p&gt;Alters the original array, &lt;strong&gt;inserts or deletes elements&lt;/strong&gt;.&lt;/p&gt;&lt;h4&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;/h4&gt;&lt;p&gt;array.splice(startIndex, deleteCount, item1, item2,...);&lt;/p&gt;&lt;h4&gt;&lt;strong&gt;Key Points&lt;/strong&gt;&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Changes the original array&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;Returns an array followed by the removed elements (if any).&lt;/li&gt;&lt;li&gt;Deleting, adding or changing elements on the spot are all possible.&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/h4&gt;&lt;h5&gt;&lt;strong&gt;a. Deleting Elements&lt;/strong&gt;&lt;/h5&gt;&lt;p&gt;const numbers = [1, 2, 3, 4, 5];&lt;/p&gt;&lt;p&gt;const removed = numbers.splice(1, 2); // Start removing from position 1 and remove 2 from that&lt;/p&gt;&lt;p&gt;console.log(removed); // [2, 3]&lt;/p&gt;&lt;p&gt;console.log(numbers); // [1, 4, 5]&lt;/p&gt;&lt;h5&gt;&lt;strong&gt;b. Adding Elements&lt;/strong&gt;&lt;/h5&gt;&lt;p&gt;const colors = [&#39;red&#39;, &#39;blue&#39;];&lt;/p&gt;&lt;p&gt;colors.splice(1, 0, &#39;green&#39;); // Add &#39;green&#39; after index 1 without deleting anything&lt;/p&gt;&lt;p&gt;console.log(colors); // [&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;]&lt;/p&gt;&lt;h5&gt;&lt;strong&gt;c. Replacing Elements&lt;/strong&gt;&lt;/h5&gt;&lt;p&gt;const letters = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];&lt;/p&gt;&lt;p&gt;letters.splice(1, 1, &#39;x&#39;); // Replace &#39;b&#39; with &#39;x&#39; at index 1&lt;/p&gt;&lt;p&gt;console.log(letters); // [&#39;a&#39;, &#39;x&#39;, &#39;c&#39;]&lt;/p&gt;&lt;h3&gt;&lt;strong&gt;3. The Key Differences&lt;/strong&gt;&lt;/h3&gt;&lt;h3&gt;&lt;table data-pm-slice=&quot;3 3 []&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Feature&lt;/th&gt;&lt;th&gt;slice()&lt;/th&gt;&lt;th&gt;splice()&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Mutates Original&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Return Value&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;New array of extracted elements&lt;/td&gt;&lt;td&gt;Array of removed elements (if any)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Parameters&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;(start, end)&lt;/td&gt;&lt;td&gt;(start, deleteCount, items...)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Use Case&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Copy a piece of an array&lt;/td&gt;&lt;td&gt;Add/remove elements in place&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Slice&lt;/strong&gt; Use it when you need to take a part from the array out &lt;strong&gt;without changing&lt;/strong&gt; the original.&lt;/li&gt;&lt;li&gt;Example: Generate a copy of an array:&lt;/li&gt;&lt;li&gt;const copy = arr.slice();&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Splice&lt;/strong&gt; Use it when you need to &lt;strong&gt;remove, add, or adjust&lt;/strong&gt; an array.&lt;/li&gt;&lt;li&gt;Example: Update a list dynamically, such as editing a to-do list.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;&lt;strong&gt;5. Pitfalls of Using These Two Methods&lt;/strong&gt;&lt;/h3&gt;&lt;h4&gt;&lt;strong&gt;Mixed Parameters Puzzle&lt;/strong&gt;&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;Slice (1, 3) obtains elements from &lt;strong&gt;indices 1 and 2&lt;/strong&gt; (excluding index 3).&lt;/li&gt;&lt;li&gt;Splice (1, 3) starts at &lt;strong&gt;index 1 and removes three elements&lt;/strong&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;&lt;strong&gt;Splice&#39;s Mutability&lt;/strong&gt;&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;Splice() &lt;strong&gt;changes the original array&lt;/strong&gt;, so always make sure you don&#39;t need to preserve the original data before using it.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Slice&lt;/strong&gt;: copy parts of an array without changing the original.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Splice&lt;/strong&gt;: can edit an array directly, deleting, introducing or replacing parts.&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/7607065267788025490/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/splice-vs-slice-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/7607065267788025490'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/7607065267788025490'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/splice-vs-slice-in-javascript.html' title='splice vs slice in javascript'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiUGC1DgEKmSSAnDdxuqbp3zRg1THd4Y8g0rOznhnKjbBr9TiOSzVHPd2RQvTstwMk3akr8b75bpfOnKcy9z8J9cLLeDDBaCOESN8n5jGuhgr4YXaW8VvrN6uPazWDuJg2ZdQ7te1BfaS8XSOoVRGpX27ypMNQ_Ma_5Y9w8uT2gXKJGmE6WztHw-epJqkU/s72-w640-h492-c/splice%20vs%20slice%20javascript.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-5034957939322754239</id><published>2025-03-02T17:37:00.004+05:30</published><updated>2025-03-02T17:42:03.118+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>5 Simple Ways to Copy an Array in JavaScript</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;5 Simple Ways to Copy an Array in JavaScript&lt;/b&gt;&lt;/div&gt;
&lt;p&gt;In JavaScript, there are &lt;strong&gt;5 simple ways to copy an array to another array&lt;/strong&gt;. Here are some examples and explanations.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3XlIuhrR_zK6-4S4ELKsXRE281Gm-pCS9OnZO0Glkhr8YzDfBEHzmO3aVbzINu0ljs9r3ExQ9zye87O072wumptsPZsRiU5XmZIScXKnGVef7Ah-BqcsLH94nn9hKEF6L9ec_kKJvJFziRMV_1qUvbwb3KRmXb_wMAR9ggnQeeDX4DR_zTmWXSwAtDY-d/s733/5%20Simple%20Ways%20to%20Copy%20an%20Array%20in%20JavaScript%20-%20visual%20selection.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;5 Simple Ways to Copy an Array in JavaScript&quot; border=&quot;0&quot; data-original-height=&quot;548&quot; data-original-width=&quot;733&quot; height=&quot;478&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3XlIuhrR_zK6-4S4ELKsXRE281Gm-pCS9OnZO0Glkhr8YzDfBEHzmO3aVbzINu0ljs9r3ExQ9zye87O072wumptsPZsRiU5XmZIScXKnGVef7Ah-BqcsLH94nn9hKEF6L9ec_kKJvJFziRMV_1qUvbwb3KRmXb_wMAR9ggnQeeDX4DR_zTmWXSwAtDY-d/w640-h478/5%20Simple%20Ways%20to%20Copy%20an%20Array%20in%20JavaScript%20-%20visual%20selection.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;1. Using the Spread Operator (...)&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;The newest and easiest way (ES6 and above):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const original = [1, 2, 3];
const copy = [...original];
console.log(copy); // [1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;2. Using slice()&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;A traditional method for making a shallow copy:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const original = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];
const copy = original.slice();
console.log(copy); // [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;3. Using Array.from()&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Turns an &lt;strong&gt;iterator&lt;/strong&gt; (like an array) into a new array:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const original = [10, 20, 30];
const copy = Array.from(original);
console.log(copy); // [10, 20, 30]
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;4. Using concat()&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Copies the original array by adding an empty array:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const original = [true, false, true];
const copy = original.concat();
console.log(copy); // [true, false, true]
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;5. Using map()&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Iterates through and returns every item (rare but feasible):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const original = [{ name: &#39;Alice&#39; }, { name: &#39;Bob&#39; }];
const copy = original.map(item =&amp;gt; item);
console.log(copy); // [{ name: &#39;Alice&#39; }, { name: &#39;Bob&#39; }]
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Shallow Copy vs. Deep Copy&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Shallow Copy&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;The methods above copy &lt;strong&gt;primitive values&lt;/strong&gt; (numbers, strings), but &lt;strong&gt;reference objects&lt;/strong&gt; (arrays/objects inside an array) still refer to the same memory location as the original.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const original = [{ x: 1 }];
const copy = [...original];
copy[0].x = 99; 
console.log(original[0].x); // 99 😱
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Deep Copy&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;For nested arrays/objects, use:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const deepCopy = JSON.parse(JSON.stringify(original));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This does not work for Date objects, functions, or circular references.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Which Method Should You Use?&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Use Spread Operator ([...arr]) or Array.from()&lt;/strong&gt; → Suitable for most cases.&lt;br /&gt;&lt;strong&gt;Need browser support for older code?&lt;/strong&gt; → Use slice()&lt;/p&gt;.&lt;br /&gt;&lt;strong&gt;Need a deep copy?&lt;/strong&gt; → Use JSON.parse(JSON.stringify(arr)) (with restrictions) or a library like _.cloneDeep() from Lodash.
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Common Mistake&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;🚨 &lt;strong&gt;Do not assign arrays directly!&lt;/strong&gt; This only creates a reference, not a copy.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const original = [1, 2, 3];
const badCopy = original; // Both point to the same array!
badCopy.push(4);
console.log(original); // [1, 2, 3, 4] 😱&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let me know if you need any further improvements! 😊&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/5034957939322754239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/5-simple-ways-to-copy-array-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/5034957939322754239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/5034957939322754239'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/5-simple-ways-to-copy-array-in.html' title='5 Simple Ways to Copy an Array in JavaScript'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3XlIuhrR_zK6-4S4ELKsXRE281Gm-pCS9OnZO0Glkhr8YzDfBEHzmO3aVbzINu0ljs9r3ExQ9zye87O072wumptsPZsRiU5XmZIScXKnGVef7Ah-BqcsLH94nn9hKEF6L9ec_kKJvJFziRMV_1qUvbwb3KRmXb_wMAR9ggnQeeDX4DR_zTmWXSwAtDY-d/s72-w640-h478-c/5%20Simple%20Ways%20to%20Copy%20an%20Array%20in%20JavaScript%20-%20visual%20selection.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-8685059031000520853</id><published>2025-03-02T17:27:00.006+05:30</published><updated>2025-03-02T17:33:35.933+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>Javascript versions list</title><content type='html'>&lt;p&gt;&lt;strong&gt;ESMAScript Versions and Features&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;JavaScript, a programming language that defines the Web, has evolved significantly since its birth in 1995. Starting as a simple scripting tool, it has grown into a diverse and powerful language.&lt;/p&gt;
&lt;p&gt;We will use &lt;strong&gt;ECMAScript (ES)&lt;/strong&gt; standards to trace its evolution. This blog post provides an overview of JavaScript versions, their history, and key characteristics, creating a context for modern web development.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;1. Introduction: JavaScript and ECMAScript&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;JavaScript was created by &lt;strong&gt;Brendan Eich in 1995&lt;/strong&gt; for Netscape Navigator. Later, &lt;strong&gt;ECMA International&lt;/strong&gt;, a standards organization, formalized it under the &lt;strong&gt;ECMAScript (ES)&lt;/strong&gt; specification. While commonly referred to as &quot;JavaScript,&quot; ECMAScript represents the standardized version of the language.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Major Milestones:&lt;/strong&gt;&lt;/h3&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxPbBT-fgeP5NTUaDX-IK2PSKpYENOWJ6kQGE9FSwYoDctKxCdwULp17rEGZn0D4rb_nkI_WllySeYSGXjesw32KblSA254XpTvnC7qAyfzn9yFCx4lE6l8tOopO8gSp105WWNVjjVIf60Gok-PegUMhKeunjP__AgoiJL1xJi7Usa82c-0dtfcIYU0Xzs/s978/ESMAScript%20Versions%20and%20Features%20-%20visual%20selection.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;ESMAScript Versions and Features  javascript versions&quot; border=&quot;0&quot; data-original-height=&quot;813&quot; data-original-width=&quot;978&quot; height=&quot;532&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxPbBT-fgeP5NTUaDX-IK2PSKpYENOWJ6kQGE9FSwYoDctKxCdwULp17rEGZn0D4rb_nkI_WllySeYSGXjesw32KblSA254XpTvnC7qAyfzn9yFCx4lE6l8tOopO8gSp105WWNVjjVIf60Gok-PegUMhKeunjP__AgoiJL1xJi7Usa82c-0dtfcIYU0Xzs/w640-h532/ESMAScript%20Versions%20and%20Features%20-%20visual%20selection.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;h2&gt;&lt;strong&gt;2. Early Versions: Laying the Foundation&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;var for variable declaration&lt;/li&gt;
&lt;li&gt;Primitive types (string, number, boolean, null, undefined)&lt;/li&gt;
&lt;li&gt;Functions and objects&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;ES1 (1997)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;The first standardized version, ECMAScript 1, established core syntax, types, and basic functionalities such as loops and conditionals.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;var for variable declaration&lt;/li&gt;
&lt;li&gt;Primitive types (string, number, boolean, null, undefined)&lt;/li&gt;
&lt;li&gt;Functions and objects&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;ES3 (1999)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;A significant step forward, introducing features still in use today.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;try...catch for error handling&lt;/li&gt;
&lt;li&gt;Regular expressions&lt;/li&gt;
&lt;li&gt;switch statements&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;ES4 (Abandoned)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;This version was abandoned due to complexity and controversy over its many new features.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;3. ES5 (2009): Modernizing JavaScript&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;ES5 marked JavaScript&#39;s shift into the modern age.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Strict Mode&lt;/strong&gt;: Enforces safer coding practices (e.g., preventing undeclared variables).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;JSON Support&lt;/strong&gt;: JSON.parse() and JSON.stringify().&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Array Methods&lt;/strong&gt;: forEach(), map(), filter(), reduce().&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Getters/Setters&lt;/strong&gt;: Object property accessors.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// ES5 Array Method Example
var numbers = [1, 2, 3];
numbers.forEach(function(num) {
    console.log(num * 2);
});
// Output: 2, 4, 6
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;4. ES6/ES2015: The Revolution&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Released in 2015, ES6 was a game changer. It improved syntax and introduced powerful new abstractions.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;let and const&lt;/strong&gt;: Block-scoped variables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Arrow Functions&lt;/strong&gt;: Concise syntax, lexical this.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Classes&lt;/strong&gt;: Syntactic sugar over prototypes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Promises&lt;/strong&gt;: Improved asynchronous handling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Modules&lt;/strong&gt;: import /export syntax.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Template Literals&lt;/strong&gt;: Interpolated strings with ${}.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Destructuring&lt;/strong&gt;: Extracts values from arrays/objects.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// ES6 Arrow Function and Destructuring
const user = { name: &#39;Alice&#39;, age: 30 };
const greet = ({ name }) =&amp;gt; `Hello, ${name}!`;
console.log(greet(user)); // &quot;Hello, Alice!&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;5. Annual Releases: ES2016 to ES2023&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;ES2016&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Array.prototype.includes(): Check if an array contains a value.&lt;/li&gt;
&lt;li&gt;Exponentiation operator (**): 2 ** 3 = 8.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;ES2017&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Async/Await&lt;/strong&gt;: Makes asynchronous code look synchronous.&lt;/li&gt;
&lt;li&gt;Object.values() / Object.entries(): Extract data from objects.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// Async/Await Example
async function fetchData() {
    const response = await fetch(&#39;https://api.example.com/data&#39;);
    const data = await response.json();
    return data;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;ES2018&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Spread/Rest for Objects&lt;/strong&gt;: const clone = {...obj };&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Promise.finally()&lt;/strong&gt;: Executes code after promise resolution.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;ES2020&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Optional Chaining&lt;/strong&gt;: user?.address?.city (runs only if address is not null or undefined).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nullish Coalescing&lt;/strong&gt;: const value = input ?? &#39;default&#39;; (checks for null /undefined).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;ES2021&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;String.replaceAll()&lt;/strong&gt;: Replaces all instances of a substring.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Logical Assignment Operators&lt;/strong&gt;: ||=, &amp;amp;&amp;amp;=, ??=.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;ES2022&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Top-Level Await&lt;/strong&gt;: Use await outside of async functions in modules.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Class Fields&lt;/strong&gt;: Declare properties directly in classes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;ES2023&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Array.findLast()&lt;/strong&gt;: Find from last in array.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hashbang Support&lt;/strong&gt;: Standardized syntax for CLI scripts (#!/usr/bin/env node).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;6. The Rise of ES Modules and Tooling&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;ES6 introduced &lt;strong&gt;native modules&lt;/strong&gt;, replacing older patterns like CommonJS.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// Import/Export Syntax
import { Component } from &#39;react&#39;;
export default function App() {};
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Modern Tooling:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Babel&lt;/strong&gt;: Transpile modern JS to older versions for browser compatibility.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Webpack/Rollup&lt;/strong&gt;: Bundle modules for production.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TypeScript&lt;/strong&gt;: Provides static typing for JavaScript.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;7. Using Modern JavaScript Today&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Most browsers support ES6+ features, but in older environments:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Transpilation&lt;/strong&gt;: Convert modern syntax into older (e.g., Babel).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Polyfills&lt;/strong&gt;: Fill missing methods (e.g., Promise).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;8. The Future: ES2024 and Beyond&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Proposed Features for ES2024:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Records and Tuples&lt;/strong&gt;: Immutable data structures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pipeline Operator&lt;/strong&gt;: value |&amp;gt; function for functional chaining.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Decorators&lt;/strong&gt;: Standardizes meta-programming for classes/methods.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;Conclusion: Why JavaScript’s Evolution Matters&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;The evolution of JavaScript parallels the increasing complexity of the web. Each version, from &lt;strong&gt;ES5’s strict mode&lt;/strong&gt; to the &lt;strong&gt;ergonomic improvements of ES2023&lt;/strong&gt;, enables developers to write cleaner, safer, and more efficient code.&lt;/p&gt;&lt;ul&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/8685059031000520853/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/javascript-versions-list-latest.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/8685059031000520853'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/8685059031000520853'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/javascript-versions-list-latest.html' title='Javascript versions list'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxPbBT-fgeP5NTUaDX-IK2PSKpYENOWJ6kQGE9FSwYoDctKxCdwULp17rEGZn0D4rb_nkI_WllySeYSGXjesw32KblSA254XpTvnC7qAyfzn9yFCx4lE6l8tOopO8gSp105WWNVjjVIf60Gok-PegUMhKeunjP__AgoiJL1xJi7Usa82c-0dtfcIYU0Xzs/s72-w640-h532-c/ESMAScript%20Versions%20and%20Features%20-%20visual%20selection.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-805517253401260489</id><published>2025-03-02T15:40:00.021+05:30</published><updated>2025-03-10T11:32:48.936+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'> ng-template &amp; ng container in angular</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;&amp;nbsp;In an Angular application, the user interface is the fact: the tags themselves, stored in a template file. Most developers are familiar with both components and directives. However, the ng-template directive is a powerful but often misunderstood tool. This blog will teach you what ng-template is, provide some examples, and show how it lets Angular applications display advanced UI patterns.&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMjRhkRCQvF3fTIiUrx1PlWV-1tWddqoYRO-N2Y8VyF3_dZ3VFNhPIY1u-7UT1JPitm13fZTz1eUpA19MQduKBgC2eKapZNBHzZqCudCYj021v0p5WsjGE9FCIvDUncehfcqF1c7BUCN8H39w4NX4VjChUguOVoGYPpdWv14aNAjWfKTvbwnCJCDEqjoaV/s917/Understanding%20ng-template%20and%20ng-container%20in%20Angular%20-%20visual%20selection.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Understanding ng-template and ng-container in Angular&quot; border=&quot;0&quot; data-original-height=&quot;466&quot; data-original-width=&quot;917&quot; height=&quot;326&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMjRhkRCQvF3fTIiUrx1PlWV-1tWddqoYRO-N2Y8VyF3_dZ3VFNhPIY1u-7UT1JPitm13fZTz1eUpA19MQduKBgC2eKapZNBHzZqCudCYj021v0p5WsjGE9FCIvDUncehfcqF1c7BUCN8H39w4NX4VjChUguOVoGYPpdWv14aNAjWfKTvbwnCJCDEqjoaV/w640-h326/Understanding%20ng-template%20and%20ng-container%20in%20Angular%20-%20visual%20selection.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h2&gt;What is ng-template?&lt;/h2&gt;
&lt;p&gt;ng-template is an Angular directive that specifies a &lt;strong&gt;template block&lt;/strong&gt; not to be rendered by the browser by default. Instead, it is treated as a blueprint for making dynamic places: when combined with structural directives such as *ngIf, *ngFor, or your own, one of the above might as well be called a register. Here is a way to save.&lt;/p&gt;
&lt;h3&gt;Characteristics&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Not Rendered at the Beginning&lt;/strong&gt;: It&#39;s as if everything inside ng-template were dull, waiting for approval.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Collaborates with Structural Directives&lt;/strong&gt;: Used to mark content which depends on something like a condition you may want to show a larger view of or data that is supposed to not just go away.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takes Contextual Information&lt;/strong&gt;: Lets you pass data to templates for dynamic rendering.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Basic Usage of ng-template &lt;/h2&gt;
&lt;h3&gt;Example 1: Conditional Rendering with *ngIf and else&lt;/h3&gt;
&lt;p&gt;This is a common use case where alternate content is shown when a particular condition isn’t true:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;div *ngIf=&quot;userLoggedIn; else loginPrompt&quot;&amp;gt;
  Welcome, {{ username }}!
&amp;lt;/div&amp;gt;

&amp;lt;ng-template #loginPrompt&amp;gt;
  &amp;lt;p&amp;gt;Please log in.&amp;lt;/p&amp;gt;
&amp;lt;/ng-template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The else clause calls the loginPrompt template.&lt;/li&gt;
&lt;li&gt;The loginPrompt template only gets rendered when userLoggedInis false.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;How It Works:&lt;/h3&gt;
&lt;p&gt;Angular converts the *ngIf syntax into:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;ng-template [ngIf]=&quot;userLoggedIn&quot;&amp;gt;
  &amp;lt;div&amp;gt;Welcome, {{ username }}!&amp;lt;/div&amp;gt;
&amp;lt;/ng-template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The #loginPrompt is a &lt;strong&gt;template reference variable&lt;/strong&gt; pointing to the ng-template.&lt;/p&gt;
&lt;h2&gt;The Role of Structural Directives&lt;/h2&gt;
&lt;p&gt;Structural directives (e.g., *ngIf, *ngFor) manipulate the DOM by adding or removing elements. As it turns out, they use ng-template to define the content they manage.&lt;/p&gt;
&lt;h3&gt;Example 2: How *ngFor Uses ng-template &lt;/h3&gt;
&lt;p&gt;This code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;ul&amp;gt;
  &amp;lt;li *ngFor=&quot;let item of items; let i = index&quot;&amp;gt;{{ i }}: {{ item }}&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Turns into this thanks to Angular:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;ul&amp;gt;
  &amp;lt;ng-template ngFor let-item [ngForOf]=&quot;items&quot; let-i=&quot;index&quot;&amp;gt;
    &amp;lt;li&amp;gt;{{ i }}: {{ item }}&amp;lt;/li&amp;gt;
  &amp;lt;/ng-template&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The contents of ng-template provide the structure that gets repeated for each item in items .&lt;/p&gt;
&lt;h2&gt;Advanced Use Cases&lt;/h2&gt;
&lt;h3&gt;1. &lt;strong&gt;Dynamic Templates with ngTemplateOutlet&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use ngTemplateOutlet to render a template dynamically, optionally with context data:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;@Component({
  template: `
    &amp;lt;ng-container *ngTemplateOutlet=&quot;greetingTemplate; context: { $implicit: &#39;User&#39; }&quot;&amp;gt;
    &amp;lt;/ng-container&amp;gt;

    &amp;lt;ng-template #greetingTemplate let-message&amp;gt;
      Welcome, {{ message }}!
    &amp;lt;/ng-template&amp;gt;
  `
})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ngTemplateOutlet displays greetingTemplate, together with context information.&lt;/li&gt;
&lt;li&gt;let-message gets the context&#39;s $implicit value.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;2. &lt;strong&gt;Custom Structural Directives&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Create reusable directives that leverage ng-template:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;@Directive({
  selector: &#39;[appRepeat]&#39;
})
export class RepeatDirective {
  constructor(
    private templateRef: TemplateRef&amp;lt;any&amp;gt;,
    private viewContainer: ViewContainerRef
  ) {}

  @Input() set appRepeat(times: number) {
    this.viewContainer.clear();
    for (let i = 0; i &amp;lt; times; i++) {
      this.viewContainer.createEmbeddedView(this.templateRef, { $implicit: i + 1 });
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Usage:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;ul&amp;gt;
  &amp;lt;li *appRepeat=&quot;3&quot;&amp;gt;Item {{ count }}&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;pre style=&quot;text-align: left;&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;ul&amp;gt;
  &amp;lt;li&amp;gt;Item 1&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;Item 2&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;Item 3&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;3. Content Projection with ng-content and Templates&lt;/b&gt;&lt;br /&gt;&lt;span&gt;Pass templates to components as inputs for flexible content projection.&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;Parent Component:&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;p&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;&amp;lt;app-card&amp;gt;&lt;br /&gt;  &amp;lt;ng-template #header&amp;gt;My Custom Header&amp;lt;/ng-template&amp;gt;&lt;br /&gt;  &amp;lt;ng-template #body&amp;gt;Body: {{ data }}&amp;lt;/ng-template&amp;gt;&lt;br /&gt;&amp;lt;/app-card&amp;gt;&lt;br /&gt;@Component({&lt;br /&gt;  selector: &#39;app-card&#39;,&lt;br /&gt;  template: `&lt;br /&gt;    &amp;lt;div class=&quot;card&quot;&amp;gt;&lt;br /&gt;      &amp;lt;ng-container *ngTemplateOutlet=&quot;headerTemplate&quot;&amp;gt;&amp;lt;/ng-container&amp;gt;&lt;br /&gt;      &amp;lt;ng-container *ngTemplateOutlet=&quot;bodyTemplate; context: { data: cardData }&quot;&amp;gt;&amp;lt;/ng-container&amp;gt;&lt;br /&gt;    &amp;lt;/div&amp;gt;&lt;br /&gt;  `,&lt;br /&gt;})&lt;br /&gt;export class CardComponent {&lt;br /&gt;  @ContentChild(&#39;header&#39;) headerTemplate!: TemplateRef&amp;lt;any&amp;gt;;&lt;br /&gt;  @ContentChild(&#39;body&#39;) bodyTemplate!: TemplateRef&amp;lt;any&amp;gt;;&lt;br /&gt;  cardData = &#39;Sample data&#39;;&lt;br /&gt;}&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;p&gt;&lt;span&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;&lt;span style=&quot;font-weight: 700;&quot;&gt;ng-template vs. ng-container&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Feature&lt;/th&gt;&lt;th&gt;ng-template&lt;/th&gt;&lt;th&gt;ng-container&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Rendering&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Not rendered by default&lt;/td&gt;&lt;td&gt;Rendered as a lightweight container&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Use Case&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Define reusable template blocks&lt;/td&gt;&lt;td&gt;Group elements without adding extra DOM nodes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Structural Directives&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Requires a directive to render&lt;/td&gt;&lt;td&gt;Can host structural directives directly&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;Best Practices&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;Child Component (app-card):&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;ul style=&quot;font-weight: normal;&quot;&gt;&lt;li&gt;&lt;p&gt;&lt;span&gt;&lt;span&gt;Avoid Overuse of Templates: Use ng-template only when necessary to keep the DOM clean.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;span&gt;&lt;span&gt;Pass Data Through Context: Use context objects for dynamic template rendering.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;span&gt;&lt;span&gt;Combine with ng-container: Use ng-container for grouping structural directives to prevent unnecessary DOM elements.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;/div&gt;&lt;/h3&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/805517253401260489/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/ng-template-ng-container-in-angular.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/805517253401260489'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/805517253401260489'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/ng-template-ng-container-in-angular.html' title=' ng-template &amp; ng container in angular'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMjRhkRCQvF3fTIiUrx1PlWV-1tWddqoYRO-N2Y8VyF3_dZ3VFNhPIY1u-7UT1JPitm13fZTz1eUpA19MQduKBgC2eKapZNBHzZqCudCYj021v0p5WsjGE9FCIvDUncehfcqF1c7BUCN8H39w4NX4VjChUguOVoGYPpdWv14aNAjWfKTvbwnCJCDEqjoaV/s72-w640-h326-c/Understanding%20ng-template%20and%20ng-container%20in%20Angular%20-%20visual%20selection.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-7561823856601947602</id><published>2025-03-02T15:19:00.005+05:30</published><updated>2025-03-02T15:23:53.005+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'> What is changedetector and its use in agular</title><content type='html'>&lt;p&gt;&amp;nbsp;Throwing Yourself into ChangeDetector in Angular: Sinking Your Doe into Performance&lt;/p&gt;
&lt;p&gt;When traditional web frameworks make the user interface (UI) just one big application that responds to all its own new data, the main job of change detection is to stop this from breaking down. In Angular, this duty is managed by an advanced change detection mechanism.&lt;/p&gt;
&lt;p&gt;The core of this mechanism is ChangeDetectorRef, a powerful tool for optimizing performance and regulating how and when Angular updates the DOM. In this article, we will examine what ChangeDetectorRef can do, how it does it, and how it is used in practice.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;What is Change Detection in Angular?&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Before sinking into ChangeDetectorRef, let’s get an initial understanding of change detection.&lt;/p&gt;
&lt;p&gt;Angular applications are dynamic: data changes over time owing to user interactions, API calls, or via timers. When data changes, Angular needs to update the DOM so that it reflects the new state of affairs. This job of synchronizing the data model with the UI is called change detection.&lt;/p&gt;
&lt;p&gt;Angular’s default change detection strategy observes all components in the application every time an event occurs (e.g., click, HTTP response, or timer tick). While this method ensures accuracy, it can become inefficient as the application size and component tree structure increase since it requires all components to be checked.&lt;/p&gt;
&lt;p&gt;To address this problem, Angular provides help for speeding up certain aspects of the transfer of information, such as the ChangeDetectorRef class.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;What is ChangeDetectorRef?&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;ChangeDetectorRef is a class provided by Angular’s @angular/core module. It allows developers to manage a component’s change detection system directly. By using ChangeDetectorRef, you can optimize performance by reducing unnecessary checks or forcing updates when needed.&lt;/p&gt;
&lt;p&gt;Key Methods of ChangeDetectorRef&lt;/p&gt;
&lt;p&gt;Let’s look at the four principal methods of ChangeDetectorRef:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;b&gt;detectChanges()&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;What it does: Triggers change detection immediately for the present component and its children.&lt;/li&gt;
&lt;li&gt;Use case: After modifying data outside Angular’s awareness (e.g., in a setTimeout or third-party library callback), update the UI.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Component, ChangeDetectorRef } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-example&#39;,
  template: `{{ data }}`
})
export class ExampleComponent {
  data: string;

  constructor(private cdr: ChangeDetectorRef) {
    setTimeout(() =&amp;gt; {
      this.data = &#39;Updated!&#39;;
      this.cdr.detectChanges(); // Manually trigger update
    }, 1000);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;b&gt;markForCheck()&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;What it does: Marks the present component and its ancestors for check during the next change detection cycle. Used with OnPush strategy.&lt;/li&gt;
&lt;li&gt;Use case: Notify Angular to check a component when its inputs change or internal state is updated.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;@Component({
  selector: &#39;app-onpush&#39;,
  template: `{{ counter }}`,
  changeDetection: ChangeDetectionStrategy.OnPush // Optimize with OnPush
})
export class OnPushComponent {
  counter = 0;

  constructor(private cdr: ChangeDetectorRef) {}

  increment() {
    this.counter++;
    this.cdr.markForCheck(); // Schedule check for next cycle
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;b&gt;detach() and reattach()&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;What they do:
&lt;ul&gt;
&lt;li&gt;detach(): Automatically disables change detection on the component.&lt;/li&gt;
&lt;li&gt;reattach(): Re-enables it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Use case: Temporarily cease change detection within performance-critical sections.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;export class DetachExampleComponent {
  constructor(private cdr: ChangeDetectorRef) {
    this.cdr.detach(); // Disable auto-checking
  }

  updateData() {
    // Changes here won&#39;t reflect on the UI until reattached
    this.cdr.reattach(); // Re-enable checking
    this.cdr.detectChanges();
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use Cases for ChangeDetectorRef&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;b&gt;Optimizing Performance with OnPush&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The OnPush change detection strategy reduces checks down to only the present component and its ancestors by carrying out operations only when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Input properties change.&lt;/li&gt;
&lt;li&gt;A component emits an event (e.g., clicking a button).&lt;/li&gt;
&lt;li&gt;markForCheck() is called.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Using ChangeDetectorRef.markForCheck() with OnPush reduces the number of non-essential checks, which improves performance when applications grow large or have complex component structures.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;b&gt;Integrating Third-Party Libraries&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;By using non-Angular libraries (e.g., D3.js or jQuery plugins), data changes can occur outside the reach of Angular’s zone. In these cases, you have to use detectChanges() to update the UI.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;ngAfterViewInit() {
  const chart = d3.select(this.elementRef.nativeElement).append(&#39;svg&#39;)
  //... setup chart
  chart.on(&#39;click&#39;, () =&amp;gt; {
    this.selectedData = newData;
    this.cdr.detectChanges(); // Force UI update
  });
}
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;b&gt;Managing Async Operations Outside Angular’s Zone&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;By default, Angular performs change detection when async operations (e.g., setTimeout) finish. If you run code outside Angular’s zone (via NgZone.runOutsideAngular), you must use detectChanges() to update.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;constructor(private ngZone: NgZone, private cdr: ChangeDetectorRef) {
  this.ngZone.runOutsideAngular(() =&amp;gt; {
    setTimeout(() =&amp;gt; {
      this.data = &#39;Updated outside Angular!&#39;;
      this.cdr.detectChanges(); // Manual update
    }, 1000);
  });
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Best Practices and Pitfalls&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Avoid Overusing Manual Detection: Wherever possible, rely on Angular’s default mechanism. Overuse of detectChanges() will complicate debugging.&lt;/li&gt;
&lt;li&gt;Combine with OnPush: Use markForCheck() to maximize performance gains.&lt;/li&gt;
&lt;li&gt;Reattach Detached Components: Forgetting to reattach a component can cause the UI to become stale.&lt;/li&gt;
&lt;li&gt;Unsubscribe from Observables: To prevent memory leaks, always clean up subscriptions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Conclusion&lt;/p&gt;
&lt;p&gt;Angular’s ChangeDetectorRef gives developers fine-grained control over change detection, making it a powerful tool for optimizing application performance. Whether you’re refactoring operations with OnPush, integrating third-party libraries, or managing async operations outside Angular’s zone, understanding ChangeDetectorRef is essential.&lt;/p&gt;
&lt;p&gt;By strategically using detectChanges(), markForCheck(), and detach(), you can ensure that your Angular applications perform efficiently.&lt;/p&gt;
&lt;p&gt;And remember: with great power comes great responsibility—use these techniques wisely to maintain code quality and application stability.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/7561823856601947602/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/what-is-changedetector-and-its-use-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/7561823856601947602'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/7561823856601947602'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/what-is-changedetector-and-its-use-in.html' title=' What is changedetector and its use in agular'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-7267489131307436444</id><published>2025-03-02T15:07:00.009+05:30</published><updated>2025-03-10T11:42:03.117+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>  router-outlet and it&#39;s purpose</title><content type='html'>&lt;p&gt;Understanding &amp;lt;router-outlet&amp;gt; in Angular and Its Purpose&lt;/p&gt;
&lt;p&gt;In Angular, the &amp;lt;router-outlet&amp;gt; directive plays an important role in navigation and showing components dynamically based on the router configuration of the application. Let&#39;s see more about its purpose, how it works, and a few advanced use examples.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqgTEQctv8rUYqp7O0NTN8kdEbYQTAMo-1Evsl8yBEWAZvSURXVWObUKRLDn8uFchqL-IRseqbI9R3KbLFjHPEYYNdtAqJEFArpfJBeUI2kchOM2TfOsv1FoqSNNeTyTP3cvT5c4llKk0il5HIaRf40fc8qm2d7BpD6nAKtbXqT7Uro3nRUkWkQUfKg3mp/s988/Understanding%20%60router-outlet%60%20and%20Its%20Purpose%20in%20Angular.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Understanding `router-outlet` and Its Purpose in Angular&quot; border=&quot;0&quot; data-original-height=&quot;598&quot; data-original-width=&quot;988&quot; height=&quot;388&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqgTEQctv8rUYqp7O0NTN8kdEbYQTAMo-1Evsl8yBEWAZvSURXVWObUKRLDn8uFchqL-IRseqbI9R3KbLFjHPEYYNdtAqJEFArpfJBeUI2kchOM2TfOsv1FoqSNNeTyTP3cvT5c4llKk0il5HIaRf40fc8qm2d7BpD6nAKtbXqT7Uro3nRUkWkQUfKg3mp/w640-h388/Understanding%20%60router-outlet%60%20and%20Its%20Purpose%20in%20Angular.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h2&gt;What is &amp;lt;router-outlet&amp;gt;?&lt;/h2&gt;
&lt;p&gt;&amp;lt;router-outlet&amp;gt; is an Angular built-in directive used as a placeholder for dynamically loaded routed components based on the application&#39;s router configuration. It acts as a viewport where Angular inserts the content of the active route.&lt;/p&gt;
&lt;h2&gt;Purpose of &amp;lt;router-outlet&amp;gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Component Loading&lt;/strong&gt;: It allows Angular to render different components based on the URL path.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single Page Application (SPA)&lt;/strong&gt;: It helps build SPAs by loading only those parts of a page that are needed without reloading it entirely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Supports Nested Routing&lt;/strong&gt;: Multiple &amp;lt;router-outlet&amp;gt; elements can be used to create child routes and complex navigation structures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Smooth Transitions&lt;/strong&gt;: Enables seamless navigation between views without requiring a page refresh.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;How &amp;lt;router-outlet&amp;gt; Works&lt;/h2&gt;
&lt;h3&gt;Step 1: Define Routes in app-routing.module.ts&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { NgModule } from &quot;@angular/core&quot;;  
import { RouterModule, Routes } from &quot;@angular/router&quot;;  
import { HomeComponent } from &quot;./home/home.component&quot;;  
import { AboutComponent } from &quot;./about/about.component&quot;;  

const routes: Routes = [  
  { path: &quot;&quot;, component: HomeComponent },  
  { path: &quot;about&quot;, component: AboutComponent }  
];  

@NgModule({  
  imports: [RouterModule.forRoot(routes)]  
})  
export class AppRoutingModule { }  
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Step 2: Add &amp;lt;router-outlet&amp;gt;to app.component.html&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;nav&amp;gt;  
  &amp;lt;a routerLink=&quot;/&quot;&amp;gt;Home&amp;lt;/a&amp;gt;  
  &amp;lt;a routerLink=&quot;/about&quot;&amp;gt;About&amp;lt;/a&amp;gt;  
&amp;lt;/nav&amp;gt;  

&amp;lt;router-outlet&amp;gt;&amp;lt;/router-outlet&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Clicking on &lt;strong&gt;Home (/)&lt;/strong&gt; will load HomeComponent inside &amp;lt;router-outlet&amp;gt;.&lt;/li&gt;
&lt;li&gt;Clicking on &lt;strong&gt;About (/about)&lt;/strong&gt; will load AboutComponent dynamically into &amp;lt;router-outlet&amp;gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Using Multiple &amp;lt;router-outlet&amp;gt; for Nested Routing&lt;/h2&gt;
&lt;p&gt;If your application has a three-level navigation structure (Parent → Child → Grandchild), you can use multiple &amp;lt;router-outlet&amp;gt; elements.&lt;/p&gt;
&lt;h3&gt;Example: Parent-Child Routing&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;const routes: Routes = [  
  {  
    path: &#39;dashboard&#39;, component: DashboardComponent,  
    children: [  
      { path: &#39;profile&#39;, component: ProfileComponent },  
      { path: &#39;settings&#39;, component: SettingsComponent }  
    ]  
  }  
];  
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;dashboard.component.html&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;h2&amp;gt;Dashboard&amp;lt;/h2&amp;gt;  
&amp;lt;a routerLink=&quot;profile&quot;&amp;gt;Profile&amp;lt;/a&amp;gt;  
&amp;lt;a routerLink=&quot;settings&quot;&amp;gt;Settings&amp;lt;/a&amp;gt;  

&amp;lt;router-outlet&amp;gt;&amp;lt;/router-outlet&amp;gt; &amp;lt;!-- Child items are mounted here --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Enter /dashboard/profile, and DashboardComponent will be shown.&lt;/li&gt;
&lt;li&gt;ProfileComponent will be displayed inside the &amp;lt;router-outlet&amp;gt; inside DashboardComponent.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Named &amp;lt;router-outlet&amp;gt;for Multiple Views&lt;/h2&gt;
&lt;p&gt;You can use named outlets to render multiple views in different containers.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;router-outlet name=&quot;primary&quot;&amp;gt;&amp;lt;/router-outlet&amp;gt;  
&amp;lt;router-outlet name=&quot;sidebar&quot;&amp;gt;&amp;lt;/router-outlet&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way, you can create layouts with multiple views.&lt;/p&gt;
&lt;p&gt;&amp;lt;router-outlet&amp;gt;is essential in Angular for navigation and dynamic component loading. It enables the creation of complex applications with nested routes, lazy loading, and multiple views.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/7267489131307436444/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/router-outlet-and-its-purpose.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/7267489131307436444'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/7267489131307436444'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/router-outlet-and-its-purpose.html' title='  router-outlet and it&#39;s purpose'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqgTEQctv8rUYqp7O0NTN8kdEbYQTAMo-1Evsl8yBEWAZvSURXVWObUKRLDn8uFchqL-IRseqbI9R3KbLFjHPEYYNdtAqJEFArpfJBeUI2kchOM2TfOsv1FoqSNNeTyTP3cvT5c4llKk0il5HIaRf40fc8qm2d7BpD6nAKtbXqT7Uro3nRUkWkQUfKg3mp/s72-w640-h388-c/Understanding%20%60router-outlet%60%20and%20Its%20Purpose%20in%20Angular.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-294900557278531144</id><published>2025-03-01T23:56:00.006+05:30</published><updated>2025-03-11T10:06:37.567+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>Angular Pipes &amp; custom pipes</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;Master Angular Pipes: Custom &amp;amp; Standard Pipes Access Data in a Whole New Way!&lt;br /&gt;Master Angular Pipes: Introducing Data Using Built-in and Custom Pipes&lt;br /&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;b&gt;When you finish, you will:&lt;/b&gt;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;✅ Have an understanding of how pipes categorize data handling.&lt;br /&gt;
✅ Be able to use Angular&#39;s built-in pipes for date and money as well.&lt;br /&gt;
✅ Know how easy it is to fashion a custom pipe.&lt;br /&gt;
✅ Avoid common mistakes and follow best practices.&lt;/p&gt;
&lt;p&gt;Turn dirty data into something that users love!&lt;/p&gt;
&lt;h2&gt;The Importance of Angular Pipes&lt;/h2&gt;
&lt;p&gt;Pipes offer a kind of form object that you can put in a template in front of the user directly. They&#39;re essential because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Clean Code&lt;/strong&gt;: Keep formatting out of containers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Usability&lt;/strong&gt;: Mix and match types.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Human-Friendly Interface&lt;/strong&gt;: Style dates, currencies, and other text in a way that people can comprehend.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;What Are Angular Pipes?&lt;/h2&gt;
&lt;p&gt;Pipes are functions that take in a variable and return a new value after processing. In a template, they are attached to the variable by the vertical line (|) followed by the pipe name:&lt;/p&gt;
&lt;pre&gt;{{ rawData | pipeName }}
&lt;/pre&gt;
&lt;h3&gt;Built-in Pipes: The Data Swiss Army Knife&lt;/h3&gt;
&lt;p&gt;Angular provides more than &lt;strong&gt;20 convenient pipes&lt;/strong&gt;. Here is a selection of common ones:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Pipe&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;&lt;br /&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;DatePipe&lt;/td&gt;
&lt;td&gt;`{{ today |&lt;/td&gt;
&lt;td&gt;date:&#39;short&#39; }}`&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CurrencyPipe&lt;/td&gt;
&lt;td&gt;`{{ price |&lt;/td&gt;
&lt;td&gt;currency:&#39;USD&#39; }}`&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UpperCasePipe&lt;/td&gt;
&lt;td&gt;`{{ &#39;hello&#39; |&lt;/td&gt;
&lt;td&gt;uppercase }}`&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JsonPipe&lt;/td&gt;
&lt;td&gt;`{{ user |&lt;/td&gt;
&lt;td&gt;json }}`&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;Creating a Custom Pipe in 4 Steps&lt;/h2&gt;
&lt;p&gt;We want to create TruncatePipe for when we have long text and need to present it with an ellipsis (e.g., &quot;Lorem ipsum...&quot;).&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRuIouHAQChT1Ir8eLS5LLOWUv5iGkKLls34ZSuCMqnY1p-AEBMx3HWwl9wVX_p_I79xiyfDNf5NXmQtlPbqznp1Gntq8Ouif4NHlR1SZk00IH9HdTRWqSv4Pa3UfAHhyCYKdiI8yuN4MMZRw3ext7Zf2FcqFWalZm6kW9fOMkS5XrdLBIAOSvfudBLGrG/s934/pipes%20and%20custom%20pipes%20in%20angular.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;pipes and custom pipes in angular&quot; border=&quot;0&quot; data-original-height=&quot;590&quot; data-original-width=&quot;934&quot; height=&quot;404&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRuIouHAQChT1Ir8eLS5LLOWUv5iGkKLls34ZSuCMqnY1p-AEBMx3HWwl9wVX_p_I79xiyfDNf5NXmQtlPbqznp1Gntq8Ouif4NHlR1SZk00IH9HdTRWqSv4Pa3UfAHhyCYKdiI8yuN4MMZRw3ext7Zf2FcqFWalZm6kW9fOMkS5XrdLBIAOSvfudBLGrG/w640-h404/pipes%20and%20custom%20pipes%20in%20angular.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h3&gt;Step 1: Generate the Pipe&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ng generate pipe truncate
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This creates truncate.pipe.ts and a test file.&lt;/p&gt;
&lt;h3&gt;Step 2: Define the Logic&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Pipe, PipeTransform } from &#39;@angular/core&#39;;

@Pipe({ name: &#39;truncate&#39; })
export class TruncatePipe implements PipeTransform {
  transform(value: string, maxLength: number = 50, ellipsis: string = &#39;...&#39;): string {
    if (!value) return &#39;&#39;;
    return value.length &amp;gt; maxLength ? value.slice(0, maxLength) + ellipsis : value;
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Step 3: Declare the Pipe&lt;/h3&gt;
&lt;p&gt;Add to your module&#39;s declarations (or use standalone: true in Angular 15+):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;@NgModule({
  declarations: [TruncatePipe],
})
export class AppModule {}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Step 4: Use It in a Template&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;{{ longText | truncate: 30 : &quot;…&quot; }}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Input:&lt;/em&gt; &quot;With Angular pipes, data formatting is a piece of cake!&quot;&lt;br /&gt;
&lt;em&gt;Output:&lt;/em&gt; &quot;With Angular pipes, data form…&quot;&lt;/p&gt;
&lt;h2&gt;5 Real-World Custom Pipe Ideas&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Highlight Matches in Search Results&lt;/strong&gt;: Develop a pipe that can transparently highlight matching text in a search.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;File Size Formatter&lt;/strong&gt;: Change bytes into KB, MB, or GB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Emoji Converter&lt;/strong&gt;: Switch :smile: into 😃.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Password Mask&lt;/strong&gt;: Cover raw text with ••••• so a user can’t read it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temperature Converter&lt;/strong&gt;: Convert between °C and °F.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Angular Pipe Best Practices&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Keep Pipes Pure&lt;/strong&gt;: Avoid side effects (unless you really need to use impure pipes).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parameterize&lt;/strong&gt;: Design pipes so they can be customized through input (like adding maxLength to TruncatePipe).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimize Performance&lt;/strong&gt;: Pure pipes aren&#39;t unexpectedly invoked.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test Thoroughly&lt;/strong&gt;: Edge cases should be covered by Angular&#39;s own testing procedure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use with Async Data&lt;/strong&gt;: Pair the async pipe with Observables/Promises&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;{{ data$ | async | uppercase }}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Common Pitfalls to Avoid&lt;/h2&gt;
&lt;p&gt;🚫 &lt;strong&gt;Overuse of Impure Pipes&lt;/strong&gt;: They’re called with each template change detection, which can hurt performance.&lt;br /&gt;
🚫 &lt;strong&gt;Ignoring Localization&lt;/strong&gt;: For more information, link CurrencyPipe with global settings for your app.&lt;br /&gt;
🚫 &lt;strong&gt;Leaving out Error Handling&lt;/strong&gt;: Check that custom pipes are not null/undefined.&lt;/p&gt;
&lt;h2&gt;Conclusion: Add Pipes to Your Angular Apps&lt;/h2&gt;
&lt;p&gt;Pipes are Angular&#39;s secret weapon for clear, reusable data formatting. Whether you&#39;re formatting dates or creating custom logic, your users will thank you when they find clean templates.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Your Next Moves&lt;/strong&gt;:&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Replace manual output with Angular&#39;s built-in pipes in your project.&lt;/li&gt;
&lt;li&gt;Create a custom pipe for a unique requirement in your project.&lt;/li&gt;
&lt;li&gt;Pass this guide to your team!&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Ask us any further questions by adding a comment below.&lt;/p&gt;&lt;ul&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;External Links:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://angular.io/api?type=pipe&quot;&gt;Angular Pipe Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.angular-university.io/&quot;&gt;RxJS Operators vs. Angular Pipes&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/294900557278531144/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-pipes-custom-pipes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/294900557278531144'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/294900557278531144'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-pipes-custom-pipes.html' title='Angular Pipes &amp; custom pipes'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRuIouHAQChT1Ir8eLS5LLOWUv5iGkKLls34ZSuCMqnY1p-AEBMx3HWwl9wVX_p_I79xiyfDNf5NXmQtlPbqznp1Gntq8Ouif4NHlR1SZk00IH9HdTRWqSv4Pa3UfAHhyCYKdiI8yuN4MMZRw3ext7Zf2FcqFWalZm6kW9fOMkS5XrdLBIAOSvfudBLGrG/s72-w640-h404-c/pipes%20and%20custom%20pipes%20in%20angular.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-6025157930735396244</id><published>2025-03-01T23:33:00.006+05:30</published><updated>2025-03-10T23:37:54.343+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>Angular Camera Integration</title><content type='html'>&lt;p&gt;&lt;strong&gt;Angular Camera Integration&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;
&lt;strong&gt;Capture Photos and Stream Video in Your Web App&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Suppose you are making a fitness app where users scan barcodes to log their meals before the end of the day. Or perhaps you are building a social platform where users take live profile pictures in real-time—no uploads required! Camera integration in web apps enables these functionalities.&lt;/p&gt;
&lt;p&gt;How can this be done? Camera integration is a recent yet case-sensitive development. With Angular, you can:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the user&#39;s camera in your app.&lt;/li&gt;
&lt;li&gt;Display live video and capture snapshots smoothly.&lt;/li&gt;
&lt;li&gt;Handle errors and permission denials appropriately&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If your app uses a camera, it will stand out. Let’s explore how to integrate it effectively.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Step 1: Equipment Needed for Angular Projects&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Before you begin:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Install the Angular CLI.&lt;/li&gt;
&lt;li&gt;Have a basic understanding of components/services.&lt;/li&gt;
&lt;li&gt;Create a new component for camera handling:
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ng generate component camera
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;Step 2: Using the Camera&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Request Permissions and Start Streaming&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Add the following to &lt;code inline=&quot;&quot;&gt;Camera.Component.ts&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Component, OnDestroy, ViewChild, ElementRef } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-camera&#39;,
  templateUrl: &#39;./camera.component.html&#39;,
  styleUrls: [&#39;./camera.component.css&#39;]
})
export class CameraComponent implements OnDestroy {
  @ViewChild(&#39;videoElement&#39;) videoElement!: ElementRef;
  @ViewChild(&#39;canvasElement&#39;) canvasElement!: ElementRef;
  private mediaStream: MediaStream | null = null;
  photoUrl: string | null = null;

  async startCamera() {
    try {
      this.mediaStream = await navigator.mediaDevices.getUserMedia({
        video: { facingMode: &#39;user&#39; },
        audio: false
      });
      if (this.videoElement.nativeElement) {
        this.videoElement.nativeElement.srcObject = this.mediaStream;
        this.videoElement.nativeElement.play();
      }
    } catch (err) {
      console.log(&#39;Camera access denied:&#39;, err);
      alert(&#39;Please turn on camera access!&#39;);
    }
  }

  ngOnDestroy() {
    this.mediaStream?.getTracks().forEach(track =&amp;gt; track.stop());
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Step 3: Viewing the Live Video Stream&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Add the following code to &lt;code inline=&quot;&quot;&gt;Camera.Component.html&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;button (click)=&quot;startCamera()&quot;&amp;gt;Enable Camera Capture&amp;lt;/button&amp;gt;
&amp;lt;video #videoElement autoplay&amp;gt;&amp;lt;/video&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Step 4: Taking a Photo from the Stream&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Add the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;TakePhoto() {
  const video = this.videoElement.nativeElement;
  const canvas = this.canvasElement.nativeElement;
  canvas.width = video.videoWidth;
  canvas.height = video.videoHeight;
  const ctx = canvas.getContext(&#39;2d&#39;);
  ctx?.drawImage(video, 0, 0, canvas.width, canvas.height);
  this.photoUrl = canvas.toDataURL(&#39;image/png&#39;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Universal Hardware Solutions to Common Problems&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;1. Access Rights Errors&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;:
&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;HTTPS&lt;/strong&gt; in production (HTTP can be blocked by browsers).&lt;/li&gt;
&lt;li&gt;Direct users to their browser settings to enable camera access.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;2. Cross-Browser Compatibility&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Ensure support for Chrome, Firefox, and Safari.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Perform feature detection:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;if (navigator.mediaDevices &amp;amp;&amp;amp; navigator.mediaDevices.getUserMedia) {
  // Supported
} else {
  alert(&#39;This browser does not support camera access.&#39;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;3. Mobile Resizing&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Add responsive CSS:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-css&quot;&gt;video {
  width: 100%;
  max-width: 500px;
  border-radius: 8px;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;Expert Advice for Production-Ready Camera Apps&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;1. High Definition Video Capture Quality&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;getUserMedia({ video: { width: { ideal: 1920 }, height: { ideal: 1080 } } });
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;2. Add a Flash/Torch for Rear Camera&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;const track = this.mediaStream?.getVideoTracks()[0];  
track?.applyConstraints({ advanced: [{ torch: true }] });  
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;1. Browser APIs&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Chrome, Firefox, and Safari support &lt;code inline=&quot;&quot;&gt;navigator.mediaDevices&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code inline=&quot;&quot;&gt;getUserMedia()&lt;/code&gt; requests camera access and returns a &lt;code inline=&quot;&quot;&gt;MediaStream&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;2. Security Rules&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Apps must run on &lt;strong&gt;HTTPS&lt;/strong&gt; (or &lt;code inline=&quot;&quot;&gt;localhost&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Users must give explicit permission for camera access.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;3. Angular Integration&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Angular components/services wrap browser APIs for seamless reactivity.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Always clean up resources in &lt;code inline=&quot;&quot;&gt;ngOnDestroy()&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;ngOnDestroy() {
  this.mediaStream?.getTracks().forEach(track =&amp;gt; track.stop());
}&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;By combining Angular’s architecture with the &lt;code inline=&quot;&quot;&gt;MediaStream&lt;/code&gt; API, you can create camera-integrated web apps that are both secure and user-friendly. Start small—implement live video streaming, then move on to photo capture.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/6025157930735396244/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-camera-integration.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6025157930735396244'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6025157930735396244'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-camera-integration.html' title='Angular Camera Integration'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-906739278370569240</id><published>2025-03-01T23:11:00.001+05:30</published><updated>2025-03-01T23:11:09.359+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'> Angular standalone components</title><content type='html'>&lt;p&gt;&lt;strong&gt;Angular Standalone Components Make Your Code Cleaner, More Productive&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Imagine setting up an Angular component is like assembling an IKEA piece of furniture—without the instruction manual. Even a simple button can turn into a configuration nightmare with declarations, imports, and exports in NgModules.&lt;/p&gt;
&lt;p&gt;Enter &lt;strong&gt;standalone components&lt;/strong&gt;—Angular’s way of eliminating boilerplate code and making development cleaner and more maintainable.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;In this guide, you will learn:&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;✅ What standalone components are (and why they are a game-changer).&lt;br /&gt;
✅ How to craft your own Angular standalone components.&lt;br /&gt;
✅ Replacing NgModules with real-world examples.&lt;br /&gt;
✅ Tips to avoid common mistakes.&lt;/p&gt;
&lt;p&gt;Let&#39;s clean up your Angular workflow! 🚀&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Why Standalone Components Were Introduced&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;With &lt;strong&gt;Angular 14&lt;/strong&gt;, standalone components eliminate the need for NgModules when building self-contained UI widgets.&lt;/p&gt;
&lt;h3&gt;&lt;/h3&gt;
&lt;h2&gt;&lt;strong&gt;What Are Standalone Components?&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Standalone components are &lt;strong&gt;self-contained Angular components&lt;/strong&gt; that declare their own dependencies (directives, pipes, services) directly in the &lt;code inline=&quot;&quot;&gt;@Component&lt;/code&gt; decorator. They are modular chunks of UI that you can easily drop into any project.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Standalone vs. Traditional Components: A Quick Comparison&lt;/strong&gt;&lt;/h3&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Traditional Component&lt;/th&gt;
&lt;th&gt;Standalone Component&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;NgModule Required?&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Dependency Management&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Handled in NgModule&lt;/td&gt;
&lt;td&gt;Declared in component decorator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Ease of Reuse&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Requires module export&lt;/td&gt;
&lt;td&gt;Import directly anywhere&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Ideal Usage&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Large apps with shared modules&lt;/td&gt;
&lt;td&gt;Micro frontends, lazy loading&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;How to Write a Standalone Component&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Step 1: Generate the Component using &lt;code inline=&quot;&quot;&gt;--standalone&lt;/code&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Run the following Angular CLI command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ng generate component button --standalone
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Step 2: Declare Dependencies&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Modify &lt;code inline=&quot;&quot;&gt;button.component.ts&lt;/code&gt; to import and declare dependencies:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Component, Input } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;

@Component({
  selector: &#39;app-button&#39;,
  standalone: true,
  imports: [CommonModule], // Import necessary modules here
  template: `&amp;lt;button&amp;gt;{{variant}}&amp;lt;/button&amp;gt;`
})
export class ButtonComponent {
  @Input() variant: &#39;primary&#39; | &#39;secondary&#39; = &#39;primary&#39;;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Step 3: Use It Anywhere!&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;No modifications to &lt;code inline=&quot;&quot;&gt;app.module.ts&lt;/code&gt; are needed—just import it into another component:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { ButtonComponent } from &#39;./button.component&#39;;

@Component({
  standalone: true,
  imports: [ButtonComponent],
  template: `&amp;lt;app-button variant=&quot;primary&quot;&amp;gt;Click Me&amp;lt;/app-button&amp;gt;`
})
export class ExampleComponent {}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;5 Practical Use Cases for Standalone Components&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Shared UI Library&lt;/strong&gt;: Create reusable buttons, modals, and cards without requiring a shared module.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lazy-Loading Routes&lt;/strong&gt;: Load standalone components on demand to improve startup time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;External Integrations&lt;/strong&gt;: Integrate components smoothly across different teams.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Third-Party Embeds&lt;/strong&gt;: Easily embed charts, maps, or widgets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Legacy App Migration&lt;/strong&gt;: Gradually modernize legacy Angular codebases.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;&lt;strong&gt;Common Pitfalls (and How to Avoid Them)&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;🚫 &lt;strong&gt;Circular Dependencies:&lt;/strong&gt; When Component A uses Component B, and vice versa.&lt;br /&gt;
✅ &lt;strong&gt;Fix&lt;/strong&gt;: Use Angular’s &lt;code inline=&quot;&quot;&gt;forwardRef()&lt;/code&gt; or redesign your structure.&lt;/p&gt;
&lt;p&gt;🚫 &lt;strong&gt;Overusing &lt;code inline=&quot;&quot;&gt;imports&lt;/code&gt; in Decorators:&lt;/strong&gt; Unnecessarily cluttering the decorator with unused modules.&lt;br /&gt;
✅ &lt;strong&gt;Fix&lt;/strong&gt;: Only import what the component &lt;em&gt;directly&lt;/em&gt; needs.&lt;/p&gt;
&lt;p&gt;🚫 &lt;strong&gt;Forgetting to Register Services:&lt;/strong&gt; Some services may be missing at runtime.&lt;br /&gt;
✅ &lt;strong&gt;Fix&lt;/strong&gt;: Add &lt;code inline=&quot;&quot;&gt;providers: [YourService]&lt;/code&gt; to the decorator when needed.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Pro Tips for Standalone Success&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;💡 &lt;strong&gt;Start Small&lt;/strong&gt;: Convert simple components (e.g., buttons, inputs) first.&lt;br /&gt;
💡 &lt;strong&gt;Leverage Angular CLI&lt;/strong&gt;: Use the &lt;code inline=&quot;&quot;&gt;--standalone&lt;/code&gt; flag for components, directives, and pipes.&lt;br /&gt;
💡 &lt;strong&gt;Use &lt;code inline=&quot;&quot;&gt;provideRouter&lt;/code&gt; for Standalone Applications&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;bootstrapApplication(AppComponent, {
  providers: [provideRouter([{ path: &#39;home&#39;, component: HomeComponent }])]
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;💡 &lt;strong&gt;Combine with Signals&lt;/strong&gt;: Use Angular’s new reactive state management with standalone components.&lt;/p&gt;
&lt;p&gt;Standalone components &lt;strong&gt;aren&#39;t just a feature—they&#39;re the future&lt;/strong&gt; of Angular development. By ditching NgModules, developers gain &lt;strong&gt;cleaner, faster, and more maintainable code&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;&lt;br /&gt;&lt;/h3&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/906739278370569240/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-standalone-components.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/906739278370569240'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/906739278370569240'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-standalone-components.html' title=' Angular standalone components'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-6734520067022510872</id><published>2025-03-01T22:59:00.005+05:30</published><updated>2025-03-01T22:59:58.473+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>End-to-End Testing with Cypress and Angular</title><content type='html'>&lt;p&gt;End-to-End Testing using Cypress and Angular - A Beginner’s Guide to Flawless Apps&lt;/p&gt;
&lt;p&gt;The Importance of End-to-End Testing for Angular Apps&lt;/p&gt;
&lt;p&gt;E2E testing is the ultimate functionality test, simulating user interactions from start to finish. This is crucial for Angular apps because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Complex behaviors:&lt;/strong&gt; Angular handles dynamic UIs (forms, routers, APIs) that need careful inspection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;High user expectations:&lt;/strong&gt; 88% of users will abandon an app after two failures&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Faster releases:&lt;/strong&gt; Teams using E2E testing see &lt;strong&gt;fewer production errors&lt;/strong&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Cypress&lt;/strong&gt; excels here with rapid reloading, real-time automatic waits, and a clear syntax, making it a top choice for Angular developers.&lt;/p&gt;
&lt;h2&gt;Setting Up Cypress in Your Angular Project&lt;/h2&gt;
&lt;h3&gt;Prerequisites&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Angular CLI installed.&lt;/li&gt;
&lt;li&gt;A basic understanding of Angular components and services.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Step 1: Install Cypress&lt;/h3&gt;
&lt;p&gt;Run the following in your project directory:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npm install cypress --save-dev  
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Step 2: Open Cypress&lt;/h3&gt;
&lt;p&gt;Initialize Cypress and generate boilerplate files:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npx cypress open  
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Cypress creates a &lt;code inline=&quot;&quot;&gt;cypress&lt;/code&gt; folder with example tests.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Step 3: Configure Cypress for Angular&lt;/h3&gt;
&lt;p&gt;Update &lt;code inline=&quot;&quot;&gt;cypress.config.ts&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { defineConfig } from &#39;cypress&#39;;  
export default defineConfig({  
  e2e: {  
    baseUrl: &#39;http://localhost:4200&#39;, // Your Angular dev server URL  
    setupNodeEvents(on, config) {},  
    specPattern: &#39;cypress/e2e/**/*.spec.ts&#39;,  
  },  
});  
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Writing Your First E2E Test&lt;/h2&gt;
&lt;p&gt;Let&#39;s create an &lt;strong&gt;E2E test for a todo list app&lt;/strong&gt; where users can add or delete tasks.&lt;/p&gt;
&lt;h3&gt;1. Create the Test File&lt;/h3&gt;
&lt;p&gt;Write &lt;code inline=&quot;&quot;&gt;cypress/e2e/todo.spec.ts&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;describe(&#39;Todo List&#39;, () =&amp;gt; {  
  beforeEach(() =&amp;gt; {  
    cy.visit(&#39;/&#39;); // Navigate to the app homepage  
  });  
  it(&#39;can enter a new item&#39;, () =&amp;gt; {  
    cy.get(&#39;[data-cy=todo-input]&#39;).type(&#39;Learn Cypress{enter}&#39;);  
    cy.get(&#39;[data-cy=todo-list] li&#39;).should(&#39;have.length&#39;, 1);  
  });  
  it(&#39;will remove an item&#39;, () =&amp;gt; {  
    cy.get(&#39;[data-cy=todo-input]&#39;).type(&#39;Delete this{enter}&#39;);  
    cy.get(&#39;[data-cy=delete-btn]&#39;).click();  
    cy.get(&#39;[data-cy=todo-list] li&#39;).should(&#39;not.exist&#39;);  
  });  
});  
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2. Run the Test&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npx cypress open  
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Click the test file and watch Cypress execute in real-time!&lt;/p&gt;
&lt;h2&gt;5 Pro Tips for Effective Cypress Tests&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Use custom data attributes (&lt;code inline=&quot;&quot;&gt;data-cy=xyz&lt;/code&gt;)&lt;/strong&gt; to avoid brittle selectors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mock API Responses&lt;/strong&gt; using &lt;code inline=&quot;&quot;&gt;cy.intercept()&lt;/code&gt; to test without backend dependency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Hooks&lt;/strong&gt; like &lt;code inline=&quot;&quot;&gt;beforeEach&lt;/code&gt; to reset state between tests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Accessibility Testing:&lt;/strong&gt; Use &lt;code inline=&quot;&quot;&gt;cy.injectAxe()&lt;/code&gt; and &lt;code inline=&quot;&quot;&gt;cy.checkA11y()&lt;/code&gt; from the &lt;code inline=&quot;&quot;&gt;cypress-axe&lt;/code&gt; plugin.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CI/CD Integration:&lt;/strong&gt; Run tests in headless mode:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npx cypress run --headless --browser chrome  
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Most Common Cypress Pitfalls (And How to Avoid Them)&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Dynamic elements&lt;/strong&gt;: Use &lt;code inline=&quot;&quot;&gt;.contains()&lt;/code&gt; or &lt;code inline=&quot;&quot;&gt;.find()&lt;/code&gt; to handle changing elements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flaky tests&lt;/strong&gt;: Ensure stable selectors, avoid timing issues, and use &lt;code inline=&quot;&quot;&gt;cy.wait()&lt;/code&gt; wisely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Modifying app code to fit tests&lt;/strong&gt;: Never change real code just to pass tests!&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Real-World Example: Testing an Angular Auth Flow&lt;/h2&gt;
&lt;p&gt;Imagine a login process with error handling.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;describe(&#39;Login Flow&#39;, () =&amp;gt; {  
  it(&#39;should log in successfully&#39;, () =&amp;gt; {  
    cy.visit(&#39;/login&#39;);  
    cy.get(&#39;[data-cy=email]&#39;).type(&#39;user@test.com&#39;);  
    cy.get(&#39;[data-cy=password]&#39;).type(&#39;password123{enter}&#39;);  
    cy.url().should(&#39;include&#39;, &#39;/dashboard&#39;);  
  });  
  it(&#39;should show error message if login fails&#39;, () =&amp;gt; {  
    cy.intercept(&#39;POST&#39;, &#39;/api/login&#39;, { statusCode: 401 });  
    cy.visit(&#39;/login&#39;);  
    cy.get(&#39;[data-cy=username]&#39;).type(&#39;wrong@test.com&#39;);  
    cy.get(&#39;[data-cy=password]&#39;).type(&#39;wrong{enter}&#39;);  
    cy.get(&#39;[data-cy=message]&#39;).should(&#39;contain&#39;, &#39;Invalid username or password.&#39;);  
  });  
});  &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using Cypress for E2E testing ensures that your Angular app functions smoothly for real users. Early bug detection saves time, reduces stress, and builds user trust.&lt;/p&gt;&lt;ul&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;External Links:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cypress.io/&quot;&gt;Cypress Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://angular.io/guide/testing&quot;&gt;Angular Testing Best Practices&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/6734520067022510872/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/end-to-end-testing-with-cypress-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6734520067022510872'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6734520067022510872'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/end-to-end-testing-with-cypress-and.html' title='End-to-End Testing with Cypress and Angular'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-3444830897835785501</id><published>2025-03-01T22:49:00.003+05:30</published><updated>2025-03-17T18:18:29.504+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>Unit Testing in Angular services</title><content type='html'>&lt;p&gt;&lt;strong&gt;Master Unit Testing in Angular: A Beginner&#39;s Guide to Jasmine and Karma&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Introduction: Why Unit Testing is Something You Can&#39;t Ignore&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What are Jasmine and Karma?&lt;/li&gt;
&lt;li&gt;How to use them properly to test both components and services.&lt;/li&gt;
&lt;li&gt;Real examples (Yes, we will!) such as reading from an external data source or engaging in user interactions.&lt;/li&gt;
&lt;li&gt;Helpful hints for beginners and advanced users alike.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By the end, you gain the ability to write tests that handle adversity while bringing pleasure and satisfaction every time they pass!&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Jasmine &amp;amp; Karma: Descriptions &amp;amp; Connections&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Jasmine: The Testing Framework&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Jasmine is a behavior-driven development (BDD) framework for human-readable test writing. It uses natural language, making it easy to write and read tests.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Characteristic Features:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code inline=&quot;&quot;&gt;describe()&lt;/code&gt;: Groups related tests (test suites).&lt;/li&gt;
&lt;li&gt;&lt;code inline=&quot;&quot;&gt;it()&lt;/code&gt;: Defines individual test cases.&lt;/li&gt;
&lt;li&gt;&lt;code inline=&quot;&quot;&gt;expect()&lt;/code&gt;: Asserts that something will happen.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;Karma: The Test Runner&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Karma runs your tests in real browsers (Chrome, Firefox) or various &quot;headless&quot; environments. Moreover, Karma re-runs tests after changes in source code.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Why Join Them Together?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Jasmine&lt;/strong&gt; writes the tests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Karma&lt;/strong&gt; executes those tests across different browsers.&lt;/li&gt;
&lt;li&gt;Both integrate seamlessly with Angular&#39;s CLI.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;How to Set Jasmine &amp;amp; Karma Up within Angular&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;1. Angular CLI: It&#39;s All Built In&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;When starting a new Angular project, Jasmine and Karma come pre-configured. Simply check the files &lt;code inline=&quot;&quot;&gt;karma.conf.js&lt;/code&gt; and &lt;code inline=&quot;&quot;&gt;src/test.ts&lt;/code&gt; to see how things are set up.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;2. Running Your First Test&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ng test
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command launches Karma, executes all tests, and displays a live-running browser with outputs.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Writing Your First Test&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Example: Testing a Simple Service&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;We&#39;ll test a &lt;code inline=&quot;&quot;&gt;CalculatorService&lt;/code&gt; with an &lt;code inline=&quot;&quot;&gt;add()&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1: Create the Service&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// calculator.service.ts
@Injectable({
  providedIn: &#39;root&#39;
})
export class CalculatorService {
  add(a: number, b: number): number {
    return a + b;
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 2: Write the Test&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// calculator.service.spec.ts
describe(&#39;CalculatorService&#39;, () =&amp;gt; {
  let service: CalculatorService;

  beforeEach(() =&amp;gt; {
    TestBed.configureTestingModule({});
    service = TestBed.inject(CalculatorService);
  });

  it(&#39;should add two numbers&#39;, () =&amp;gt; {
    expect(service.add(2, 3)).toBe(5);
  });

  it(&#39;should handle negative numbers&#39;, () =&amp;gt; {
    expect(service.add(-1, 5)).toBe(4);
  });
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 3: Run the Test&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ng test
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Karma reports back with either green checks (for passing tests) or red Xs (for failures).&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Real Test Scenarios&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;1. Components with User Interaction&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;This test checks if a &lt;code inline=&quot;&quot;&gt;LoginComponent&lt;/code&gt; emits its &lt;code inline=&quot;&quot;&gt;login&lt;/code&gt; event when the button is clicked.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// login.component.spec.ts
it(&#39;should emit login event on button click&#39;, () =&amp;gt; {
  spyOn(component.login, &#39;emit&#39;);
  const button = fixture.nativeElement.querySelector(&#39;button&#39;);
  button.click();
  expect(component.login.emit).toHaveBeenCalled();
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;2. Mocking HTTP Requests&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Testing a &lt;code inline=&quot;&quot;&gt;DataService&lt;/code&gt; that fetches data from an API using &lt;code inline=&quot;&quot;&gt;HttpClientTestingModule&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// data.service.spec.ts
import { HttpClientTestingModule, HttpTestingController } from &#39;@angular/common/http/testing&#39;;

describe(&#39;DataService&#39;, () =&amp;gt; {
  let httpMock: HttpTestingController;

  beforeEach(() =&amp;gt; {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule]
    });
    httpMock = TestBed.inject(HttpTestingController);
  });

  it(&#39;should fetch users&#39;, () =&amp;gt; {
    const mockUsers = [{ id: 1, name: &#39;John&#39; }];
    service.getUsers().subscribe(users =&amp;gt; {
      expect(users).toEqual(mockUsers);
    });
    const req = httpMock.expectOne(&#39;/api/users&#39;);
    req.flush(mockUsers);
    httpMock.verify();
  });
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Best Practices for Successful Testing&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Make each &lt;code inline=&quot;&quot;&gt;it()&lt;/code&gt; focused:&lt;/strong&gt; Tests should be concise and targeted.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Descriptive Names:&lt;/strong&gt; &lt;code inline=&quot;&quot;&gt;it(&#39;should print error when email is invalid&#39;)&lt;/code&gt; &amp;gt; &lt;code inline=&quot;&quot;&gt;it(&#39;testing form&#39;)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mock Dependencies:&lt;/strong&gt; Use spies or stubs to isolate tests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prioritize Critical Paths:&lt;/strong&gt; Focus on areas with the highest user impact.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Always Run Tests:&lt;/strong&gt; Integrate tests into CI/CD pipelines (e.g., GitHub Actions).&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;&lt;br /&gt;&lt;/h2&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/3444830897835785501/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/unit-testing-in-angular-services.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/3444830897835785501'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/3444830897835785501'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/unit-testing-in-angular-services.html' title='Unit Testing in Angular services'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-8809704371389267782</id><published>2025-03-01T22:42:00.002+05:30</published><updated>2025-03-10T23:38:04.814+05:30</updated><title type='text'>Angular Unit Testing Mastery: Jasmine, Karma, and Component Testing Explained</title><content type='html'>&lt;p&gt;&lt;strong&gt;Angular Unit Testing Mastery: Jasmine, Karma, and Component Testing Explained&lt;/strong&gt;&lt;/p&gt;&lt;h1&gt;&lt;strong&gt;Angular Unit Testing Mastery: Jasmine, Karma, and Component Testing Explained&lt;/strong&gt;&lt;/h1&gt;&lt;h1&gt;&lt;strong&gt;Why Component Testing Is Crucial&lt;/strong&gt;&lt;/h1&gt;
&lt;p&gt;You’ve written a stunning Angular component. It looks perfect, but do you know if it produces the expected results?&lt;br /&gt;
Manual testing is too time-consuming and prone to error.&lt;/p&gt;
&lt;p&gt;Component testing with Jasmine and Karma automates the process, allowing you to validate your UI logic and user interactions in milliseconds.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;This guide includes the following topics:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;How to test Angular &lt;strong&gt;components&lt;/strong&gt; (including inputs, outputs, and DOM interactions)&lt;/li&gt;
&lt;li&gt;Advanced techniques on services, HTTP calls, and user events&lt;/li&gt;
&lt;li&gt;Tools such as TestBed, ComponentFixture, and Spies&lt;/li&gt;
&lt;li&gt;Real-world examples to bulletproof your app.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;Step by Step for Component Testing&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;1. Create a Simple Counter Component&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Let&#39;s write a CounterComponent with increment/decrement buttons.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// counter.component.ts
@Component({
  selector: &#39;app-counter&#39;,
  template: ` 
    &amp;lt;button (click)=&quot;decrement()&quot;&amp;gt;-&amp;lt;/button&amp;gt;
    {{ count }}
    &amp;lt;button (click)=&quot;increment()&quot;&amp;gt;+&amp;lt;/button&amp;gt;
  `
})
export class CounterComponent {
  count = 0;
  increment() { this.count++; }
  decrement() { this.count--; }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;2. Write Test for Component&#39;s Initial State, Button Click, and UI Updates&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// counter.component.spec.ts
describe(&#39;CounterComponent&#39;, () =&amp;gt; {
  let component: CounterComponent;
  let fixture: ComponentFixture&amp;lt;CounterComponent&amp;gt;;

  beforeEach(async () =&amp;gt; {
    await TestBed.configureTestingModule({
      declarations: [CounterComponent]
    }).compileComponents();

    fixture = TestBed.createComponent(CounterComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it(&#39;should start with count 0&#39;, () =&amp;gt; {
    expect(component.count).toBe(0);
  });

  it(&#39;should increment count when + is clicked&#39;, () =&amp;gt; {
    const button = fixture.nativeElement.querySelector(&#39;button:last-child&#39;);
    button.click();
    fixture.detectChanges();
    expect(component.count).toBe(1);
    expect(fixture.nativeElement.querySelector(&#39;span&#39;).textContent).toBe(&#39;1&#39;);
  });

  it(&#39;should decrement count when - is clicked&#39;, () =&amp;gt; {
    component.count = 5;
    fixture.detectChanges();
    const button = fixture.nativeElement.querySelector(&#39;button:first-child&#39;);
    button.click();
    expect(component.count).toBe(4);
  });
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Take Home Points:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;TestBed&lt;/strong&gt;: Sets up the test environment (like NgModule).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ComponentFixture&lt;/strong&gt;: Wraps component instance and template.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;detectChanges()&lt;/strong&gt;: Makes Angular detect changes and update the view.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;Testing Component Inputs and Outputs&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Example: Notification Banner Component&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Test a component that takes an @Input() message and emits an @Output()on dismissal.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// notification.component.ts
@Component({
  selector: &#39;app-notification&#39;,
  template: `
    &amp;lt;div&amp;gt;
      {{ message }}
      &amp;lt;button (click)=&quot;dismiss()&quot;&amp;gt;Close&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  `
})
export class NotificationComponent {
  @Input() message: string;
  @Output() closed = new EventEmitter&amp;lt;void&amp;gt;();

  dismiss() {
    this.closed.emit();
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Test Cases&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// notification.component.spec.ts
it(&#39;should display the input message&#39;, () =&amp;gt; {
  component.message = &#39;Hello!&#39;;
  fixture.detectChanges();
  const div = fixture.nativeElement.querySelector(&#39;div&#39;);
  expect(div.textContent).toContain(&#39;Hello!&#39;);
});

it(&#39;should emit event when dismissed&#39;, () =&amp;gt; {
  component.message = &#39;Test&#39;;
  fixture.detectChanges();
  spyOn(component.closed, &#39;emit&#39;);
  const button = fixture.nativeElement.querySelector(&#39;button&#39;);
  button.click();
  expect(component.closed.emit).toHaveBeenCalled();
});
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Testing Component with Dependencies&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Test a UserProfileComponent that relies on a UserService&lt;/strong&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;strong&gt;1. Mock the Dependency&lt;/strong&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;// user.service.mock.ts
class MockUserService {
  getUser() {
    return of({ name: &#39;Alice&#39;, email: &#39;alice@test.com&#39; });
  }
}

// user-profile.component.spec.ts
beforeEach(() =&amp;gt; {
  TestBed.configureTestingModule({
    declarations: [UserProfileComponent],
    providers: [
      { provide: UserService, useClass: MockUserService }
    ]
  });

  fixture = TestBed.createComponent(UserProfileComponent);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;&lt;strong&gt;2. Test Data Rendering&lt;/strong&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;it(&#39;should display user data&#39;, () =&amp;gt; {
  fixture.detectChanges(); // Triggers ngOnInit
  const nameEl = fixture.nativeElement.querySelector(&#39;.name&#39;);
  expect(nameEl.textContent).toContain(&#39;Alice&#39;);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Best Practices for Component Testing&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Test Templates and Logic&lt;/strong&gt;: Validate both UI and component class logic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use async/fakeAsync&lt;/strong&gt;: Handle timers and asynchronous operations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Leverage Angular Utilities&lt;/strong&gt;: DebugElement, By.css(), and triggerEventHandler().&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Isolate Tests&lt;/strong&gt;: Mock services to avoid HTTP calls or state leaks.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;&lt;strong&gt;Advanced Techniques&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;Simulating User Input with fakeAsync&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Test a form input with debounce:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { fakeAsync, tick } from &#39;@angular/core/testing&#39;;

it(&#39;should update search term after debounce&#39;, fakeAsync(() =&amp;gt; {
  const input = fixture.nativeElement.querySelector(&#39;input&#39;);
  input.value = &#39;test&#39;;
  input.dispatchEvent(new Event(&#39;input&#39;));
  tick(300); // Fast-forward 300ms
  expect(component.searchTerm).toBe(&#39;test&#39;);
}));
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Code Coverage Reports&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Generate coverage stats:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ng test --code-coverage
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Open coverage/index.html to see which paths lack test coverage.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Conclusion: Key Point Rating&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt; &lt;strong&gt;Testing components by simulating clicks, inputs, and outputs&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;TestBed for module setup and dependency simulation&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;Combine unit tests with integration tests for full code coverage&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;&lt;br /&gt;&lt;/h3&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/8809704371389267782/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-unit-testing-mastery-jasmine.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/8809704371389267782'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/8809704371389267782'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-unit-testing-mastery-jasmine.html' title='Angular Unit Testing Mastery: Jasmine, Karma, and Component Testing Explained'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-3449686216384369077</id><published>2025-03-01T20:21:00.010+05:30</published><updated>2025-03-11T10:02:39.704+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>Angular interceptors tutorial</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;strong&gt;Angular Interceptors Explained: A Beginner’s Guide to Global HTTP Handling&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;The Problem with Repetitive HTTP Logic&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Imagine you&#39;re creating an Angular app. Each HTTP request needs an authentication token, error handling, and logging. All the code is scattered across different services without any centralization. At this point of the game, you&#39;re smothered in byte blood—running &#39;head, tail, head, tail&#39; all day long—covering your app with technical debt.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Introducing Angular Interceptors&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Middleware that stands between your app and the server, interceptors allow you to modify requests, handle errors globally, and streamline tasks such as logging. This guide ensures that you replace this verbose mess of code with clear, reusable interceptors before the end of this book. Let&#39;s get started!&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj7YvHFC70OqIVg72b8xM97Llz0oa5lsgy8covwFxI5SSNScHXD6rpfSBR9y1gcl_i5fkbwzOSsvE3b9Vj6Qrp8DDRnSSBnQQDetvYiJHtCmcG53XBGacwQOo27reCrEvvre_FGxp1zA8A_n-eWE6eyUYszw3P2Mu_EkjLgzuDgT-XBT0fb1oPnbU-vVqJ/s973/Angular%20Interceptors%20Explained_%20A%20Beginner%E2%80%99s%20Guide%20to%20Global%20HTTP%20Handling.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Angular Interceptors Explained_ A Beginner’s Guide to Global HTTP Handling&quot; border=&quot;0&quot; data-original-height=&quot;575&quot; data-original-width=&quot;973&quot; height=&quot;378&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj7YvHFC70OqIVg72b8xM97Llz0oa5lsgy8covwFxI5SSNScHXD6rpfSBR9y1gcl_i5fkbwzOSsvE3b9Vj6Qrp8DDRnSSBnQQDetvYiJHtCmcG53XBGacwQOo27reCrEvvre_FGxp1zA8A_n-eWE6eyUYszw3P2Mu_EkjLgzuDgT-XBT0fb1oPnbU-vVqJ/w640-h378/Angular%20Interceptors%20Explained_%20A%20Beginner%E2%80%99s%20Guide%20to%20Global%20HTTP%20Handling.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h3&gt;&lt;strong&gt;What Are Angular Interceptors?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Interceptors are classes that implement HttpInterceptor. They &lt;strong&gt;intercept&lt;/strong&gt; HTTP requests or responses before the request is made to the server. Conversely, they intercept the response before it reaches your app.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;Step 1: Creating Your First Interceptor&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;We&#39;ll make an interceptor which adds a token to the header of every HTTP request.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;1a. Generate the Interceptor&lt;/strong&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ng generate interceptor auth  
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;&lt;strong&gt;1b. Implement the Intercept Method&lt;/strong&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Injectable } from &#39;@angular/core&#39;;  
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from &#39;@angular/common/http&#39;;  
import { Observable } from &#39;rxjs&#39;;  

@Injectable()  
export class AuthInterceptor implements HttpInterceptor {  
  intercept(req: HttpRequest&amp;lt;any&amp;gt;, next: HttpHandler): Observable&amp;lt;HttpEvent&amp;lt;any&amp;gt;&amp;gt; {  
    // Clone the request and add the auth header  
    const authToken = &#39;YOUR_TOKEN&#39;;  
    const authReq = req.clone({  
      setHeaders: { Authorization: `Bearer ${authToken}` }  
    });  
    return next.handle(authReq);  
  }  
}  
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;&lt;strong&gt;1c. Register the Interceptor&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Add it to your app’s providers in app.module.ts:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { HTTP_INTERCEPTORS } from &#39;@angular/common/http&#39;;  
import { AuthInterceptor } from &#39;./auth.interceptor&#39;;  

@NgModule({  
  providers: [  
    { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }  
  ]  
})  
export class AppModule {}  
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Now, every HTTP request from your app includes the token automatically!&lt;/strong&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;h3&gt;&lt;strong&gt;Real-World Use Cases for Interceptors&lt;/strong&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;strong&gt;1. Global Error Handling&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;If an HTTP request results in an error (e.g., 404, 500), intercept it and replace the message that&#39;s sent back to users with something user-friendly.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { catchError } from &#39;rxjs/operators&#39;;  
import { throwError } from &#39;rxjs&#39;;  

intercept(req: HttpRequest&amp;lt;any&amp;gt;, next: HttpHandler): Observable&amp;lt;HttpEvent&amp;lt;any&amp;gt;&amp;gt; {  
  return next.handle(req).pipe(  
    catchError(error =&amp;gt; {  
      console.error(&#39;Error:&#39;, error);  
      alert(&#39;Something went wrong!&#39;);  
      return throwError(() =&amp;gt; error);  
    })  
  );  
}  
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;&lt;strong&gt;2. Logging HTTP Activity&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;To keep track of when you made requests and which URLs were addressed.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;3. Modify Responses&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Modify the data returned before loading it into a component.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;intercept(req: HttpRequest&amp;lt;any&amp;gt;, next: HttpHandler): Observable&amp;lt;HttpEvent&amp;lt;any&amp;gt;&amp;gt; {  
  return next.handle(req).pipe(  
    map(event =&amp;gt; {  
      if (event instanceof HttpResponse) {  
        return event.clone({ body: event.body.data }); // Unwrap nested data  
      }  
      return event;  
    })  
  );  
}  
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;Interceptor Best Practices&lt;/strong&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Clone Requests&lt;/strong&gt;: Always clone the request before modifying it to prevent side effects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Order Matters&lt;/strong&gt;: Requests are modified in the sequence they&#39;re passed to HTTP_INTERCEPTORS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use multi: true&lt;/strong&gt;: Allows multiple interceptors in your app at once.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoid Memory Leaks&lt;/strong&gt;: Use takeUntil to unsubscribe from consumer services.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;&lt;strong&gt;Common Pitfalls and How to Avoid Them&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Don&#39;t make changes to the original request&lt;/strong&gt;: Always use clone().&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Deal with errors properly&lt;/strong&gt;: Always return throwError after intercepting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep Interceptors Focused&lt;/strong&gt;: Separate interceptors for different functionalities (e.g., authentication, logging).&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/3449686216384369077/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-interceptors-tutorial.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/3449686216384369077'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/3449686216384369077'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/angular-interceptors-tutorial.html' title='Angular interceptors tutorial'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj7YvHFC70OqIVg72b8xM97Llz0oa5lsgy8covwFxI5SSNScHXD6rpfSBR9y1gcl_i5fkbwzOSsvE3b9Vj6Qrp8DDRnSSBnQQDetvYiJHtCmcG53XBGacwQOo27reCrEvvre_FGxp1zA8A_n-eWE6eyUYszw3P2Mu_EkjLgzuDgT-XBT0fb1oPnbU-vVqJ/s72-w640-h378-c/Angular%20Interceptors%20Explained_%20A%20Beginner%E2%80%99s%20Guide%20to%20Global%20HTTP%20Handling.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-3281220308871273961</id><published>2025-03-01T19:58:00.007+05:30</published><updated>2025-03-17T17:47:38.178+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>HTTP Requests with HttpClient in Angular</title><content type='html'>&lt;p&gt;How to Make HTTP Requests with HttpClient in Angular&lt;/p&gt;
&lt;p&gt;With the rise of modern web development, the ability to communicate with APIs has become a vital component of your Angular application. Angular has a built-in and powerful service called &lt;strong&gt;HttpClient&lt;/strong&gt; for making HTTP calls.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;HttpClient&lt;/strong&gt; provides out-of-the-box methods that make it easier to:&lt;br /&gt;
 Retrieve data from an API&lt;br /&gt;
 Send form data&lt;br /&gt;
 Handle errors efficiently&lt;/p&gt;
&lt;p&gt;In this guide, we will cover:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How to configure and use &lt;strong&gt;HttpClient&lt;/strong&gt; in Angular&lt;/li&gt;
&lt;li&gt;How to deal with API calls efficiently&lt;/li&gt;
&lt;li&gt;Separating out HTTP logic into a &lt;strong&gt;service&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Doing subscription logic inside a &lt;strong&gt;component&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;h2&gt;Getting Started with HttpClient in Angular&lt;/h2&gt;
&lt;h3&gt;Step 1 — Import HttpClientModule&lt;/h3&gt;
&lt;p&gt;To start using &lt;strong&gt;HttpClient&lt;/strong&gt;, you first need to import the HttpClientModule in your &lt;strong&gt;app module&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;Add HttpClientModule to app.module.ts:&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7mHHyua25FOPak1gB0H-ejV8FcYrfDW7IGpJLeGiN-vCXbAUxfI8aWwP_EjTtLu7X-2vrg6VyFU9WMO62G_Kq4tmy07WymLRAdTTvi1z6m_jI6zVaG5Zv1kmNP4MbMc2Gg6xxNeJPul7GdY3yrAJ5V2XVXKzTXkNun5ufVlr6yEzeOPgKduvk5fcwk-td/s911/HTTP%20Requests%20with%20HttpClient%20in%20Angular.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;HTTP Requests with HttpClient in Angular&quot; border=&quot;0&quot; data-original-height=&quot;540&quot; data-original-width=&quot;911&quot; height=&quot;380&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7mHHyua25FOPak1gB0H-ejV8FcYrfDW7IGpJLeGiN-vCXbAUxfI8aWwP_EjTtLu7X-2vrg6VyFU9WMO62G_Kq4tmy07WymLRAdTTvi1z6m_jI6zVaG5Zv1kmNP4MbMc2Gg6xxNeJPul7GdY3yrAJ5V2XVXKzTXkNun5ufVlr6yEzeOPgKduvk5fcwk-td/w640-h380/HTTP%20Requests%20with%20HttpClient%20in%20Angular.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, you can use &lt;strong&gt;HttpClient&lt;/strong&gt; in your Angular application.&lt;/p&gt;
&lt;p&gt;To keep the code &lt;strong&gt;clean and maintainable&lt;/strong&gt;, move the API calls to an Angular &lt;strong&gt;service&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;Step 2: Setting Up a Data Service&lt;/h2&gt;
&lt;p&gt;Run the following command to generate a new service:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sh&quot;&gt;ng g s data
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will create a data.service.ts file. Open the file and add the following code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Injectable } from &#39;@angular/core&#39;;
import { HttpClient } from &#39;@angular/common/http&#39;;
import { Observable } from &#39;rxjs&#39;;

@Injectable({
  providedIn: &#39;root&#39;,
})
export class DataService {
  private apiUrl = &#39;https://jsonplaceholder.typicode.com/posts&#39;;

  constructor(private http: HttpClient) {}

  getPosts(): Observable&amp;lt;any[]&amp;gt; {
    return this.http.get&amp;lt;any[]&amp;gt;(this.apiUrl);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Why Move API Calls to a Service?&lt;/h3&gt;
&lt;p&gt; Keeps &lt;strong&gt;components clean&lt;/strong&gt; and focused only on UI logic&lt;br /&gt;
 Encourages &lt;strong&gt;reusability&lt;/strong&gt; of API calls&lt;br /&gt;
 Makes &lt;strong&gt;unit testing&lt;/strong&gt; easier&lt;/p&gt;
&lt;h2&gt;Step 3: Subscribing to the API in a Component&lt;/h2&gt;
&lt;p&gt;Now, we will use the &lt;strong&gt;service&lt;/strong&gt; inside a component. Update app.component.ts to subscribe to the API response:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Component, OnInit } from &#39;@angular/core&#39;;
import { DataService } from &#39;../data.service&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;,
  styleUrls: [&#39;./app.component.css&#39;],
})
export class AppComponent implements OnInit {
  posts: any[] = [];

  constructor(private dataService: DataService) {}

  ngOnInit(): void {
    this.dataService.getPosts().subscribe(
      (response) =&amp;gt; {
        this.posts = response;
      },
      (error) =&amp;gt; {
        console.error(&#39;API Error:&#39;, error);
      }
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;What’s Happening Here?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;ngOnInit()&lt;/strong&gt; lifecycle hook ensures data is fetched &lt;strong&gt;when the component loads&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;subscribe()&lt;/strong&gt; method listens for the API response and updates the posts array.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Step 4: Displaying Data in the Template&lt;/h2&gt;
&lt;p&gt;Now, update app.component.html to display the blog posts dynamically:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;h2&amp;gt;Blog Posts&amp;lt;/h2&amp;gt;
&amp;lt;ul&amp;gt;
  &amp;lt;li *ngFor=&quot;let post of posts&quot;&amp;gt;
    &amp;lt;strong&amp;gt;{{ post.title }}&amp;lt;/strong&amp;gt;
    &amp;lt;p&amp;gt;{{ post.body }}&amp;lt;/p&amp;gt;
  &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will &lt;strong&gt;dynamically render&lt;/strong&gt; the fetched posts in a simple list format.&lt;/p&gt;
&lt;h2&gt;Handling API Errors with RxJS&lt;/h2&gt;
&lt;p&gt;To &lt;strong&gt;gracefully handle errors&lt;/strong&gt;, update data.service.ts to use &lt;strong&gt;catchError&lt;/strong&gt; from RxJS:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { catchError } from &#39;rxjs/operators&#39;;
import { throwError } from &#39;rxjs&#39;;

getPosts(): Observable&amp;lt;any[]&amp;gt; {
  return this.http.get&amp;lt;any[]&amp;gt;(this.apiUrl).pipe(
    catchError((error) =&amp;gt; {
      console.error(&#39;API Error:&#39;, error);
      return throwError(() =&amp;gt; new Error(&#39;Something went wrong!&#39;));
    })
  );
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Why Handle Errors?&lt;/h3&gt;
&lt;p&gt; Logs API failures&lt;br /&gt;
Provides &lt;strong&gt;better user experience&lt;/strong&gt; with error messages&lt;br /&gt;
Prevents &lt;strong&gt;app crashes&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;Best Practices for HttpClient Usage&lt;/h2&gt;
&lt;p&gt; &lt;strong&gt;Move API logic to a service&lt;/strong&gt; to keep components clean.&lt;br /&gt;
&lt;strong&gt;Use RxJS operators&lt;/strong&gt; like catchError to handle errors.&lt;br /&gt;
&lt;strong&gt;Unsubscribe from observables&lt;/strong&gt; when needed to prevent memory leaks.&lt;br /&gt;
&lt;strong&gt;Store API URLs in environment variables&lt;/strong&gt; instead of hardcoding them.&lt;/p&gt;
&lt;p&gt;By now, you should be familiar with &lt;strong&gt;how to make HTTP requests with HttpClient in Angular&lt;/strong&gt;.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/3281220308871273961/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/http-requests-with-httpclient-in-angular.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/3281220308871273961'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/3281220308871273961'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/http-requests-with-httpclient-in-angular.html' title='HTTP Requests with HttpClient in Angular'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7mHHyua25FOPak1gB0H-ejV8FcYrfDW7IGpJLeGiN-vCXbAUxfI8aWwP_EjTtLu7X-2vrg6VyFU9WMO62G_Kq4tmy07WymLRAdTTvi1z6m_jI6zVaG5Zv1kmNP4MbMc2Gg6xxNeJPul7GdY3yrAJ5V2XVXKzTXkNun5ufVlr6yEzeOPgKduvk5fcwk-td/s72-w640-h380-c/HTTP%20Requests%20with%20HttpClient%20in%20Angular.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2964959029992147971.post-6601715666458674828</id><published>2025-03-01T19:46:00.007+05:30</published><updated>2025-03-02T12:42:00.926+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Angular"/><title type='text'>RxJS in Angular tutorial</title><content type='html'>&lt;p&gt;Mastering RxJS in Angular: A Complete Guide to Reactive Programming&quot;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Mastering RxJS in Angular: A Complete Guide to Reactive Programming&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Introduction: Why RxJS is Crucial for Angular Developers&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Angular apps thrive on reactivity — user inputs, API calls, and dynamic UI updates all happen asynchronously. Without a structured approach, handling these tasks would lead to tangled code or bugs. &lt;strong&gt;RxJS&lt;/strong&gt; (Reactive Extensions for JavaScript) is Angular’s secret weapon. It turns async operations into &lt;strong&gt;Observables&lt;/strong&gt;, a new, declarative way to work with async.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;This Guide Will:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Show how deeply integrated Angular and RxJS are for HTTP, forms, and state management.&lt;/li&gt;
&lt;li&gt;Cover the basics of RxJS (Observables, Operators, Subjects) in an Angular environment.&lt;/li&gt;
&lt;li&gt;Step by step create things like a real-time search bar using Angular’s HttpClient.&lt;/li&gt;
&lt;li&gt;Discuss the best practices to prevent memory leaks and boost performance for your Angular app.&lt;/li&gt;&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;RxJS in Angular: The Reactive Backbone&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Angular’s core features leverage RxJS:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HTTP Requests:&lt;/strong&gt; HttpClient methods all return Observables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reactive Forms:&lt;/strong&gt; Follow form changes with valueChanges Observables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Async Pipe:&lt;/strong&gt; Automatically subscribe/unsubscribe in templates.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;State Management:&lt;/strong&gt; NgRx, for example, makes extensive use of RxJS in actions and effects.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;strong&gt;Why RxJS + Angular?&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Many nested callbacks can be rewritten neatly with chainable operators.&lt;/li&gt;
&lt;li&gt;Using async pipe is much easier than manual unsubscriptions.&lt;/li&gt;
&lt;li&gt;Real-time features like WebSockets, search, or updates are a snap.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;Core Concepts for Angular Developers&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;1. Observables in Angular&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;An &lt;strong&gt;Observable&lt;/strong&gt; represents a stream of data. Angular uses them extensively, as in HttpClient:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { HttpClient } from &#39;@angular/common/http&#39;;
@Injectable()
export class DataService {
  constructor(private http: HttpClient) {}
  // Get Observable that produces the data
  getUsers(): Observable {
    return this.http.get(&#39;/api/users&#39;);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Subscribe to it in a Component:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;updatedUsers$: Observable = this.users$.pipe(
  map(users =&amp;gt; users.filter(u =&amp;gt; u.age &amp;gt; 30)),
  shareReplay({ bufferSize: 1, refCount: false })
);

data$ = this.dataService.getUsers().pipe(
  catchError(error =&amp;gt; of([])) // Handle mistakes gracefully
);
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;2. Operators for Angular Apps&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Use operators to transform, filter, or combine streams:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example: Debounce a Search Input&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { FormControl } from &#39;@angular/forms&#39;;
import { debounceTime, distinctUntilChanged, switchMap } from &#39;rxjs/operators&#39;;

searchControl = new FormControl(&#39;&#39;);
ngOnInit() {
  this.searchControl.valueChanges.pipe(
    debounceTime(300),
    distinctUntilChanged(),
    switchMap(query =&amp;gt; this.dataService.search(query))
  ).subscribe(results =&amp;gt; this.results = results);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;3. Subjects for Cross-Component Communication&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Subjects&lt;/strong&gt; multicast values to multiple subscribers. Use them for shared state in a service:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;import { Subject } from &#39;rxjs&#39;;
@Injectable()
export class NotificationService {
  private notificationSubject = new Subject();
  notifications$ = this.notificationSubject.asObservable();
  sendNotification(message: string) {
    this.notificationSubject.next(message);
  }
}

// Component 1: this.notificationService.sendNotification(&#39;Hello!&#39;);
// Component 2: messages$ | async
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Why RxJS + Angular? Real-World Use Cases&lt;/strong&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HTTP Request Cancellation:&lt;/strong&gt; switchMap disposing of the old API calls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Form Validation:&lt;/strong&gt; valueChanges reactive response to inputs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Event Handling:&lt;/strong&gt; Delay button clicks and window resizing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Yahoo! Case Study:&lt;/strong&gt; Google uses RxJS on its Angular Team to manage complex asynchronous workflows in the framework itself, ensuring efficiency and scalability.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Step-by-Step: Real-Time Search with Angular and RxJS&lt;/strong&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;strong&gt;1. Set Up the Service&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;@Injectable()
export class SearchService {
  constructor(private http: HttpClient) {}
  search(term: string): Observable {
    return this.http.get(`/api/search?q=${term}`);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;2. Build the Component&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;export class SearchComponent implements OnInit {
  constructor(private searchService: SearchService) {}
  ngOnInit() {
    this.searchControl.valueChanges.pipe(
      debounceTime(300),
      distinctUntilChanged(),
      switchMap(term =&amp;gt; this.searchService.search(term)),
      catchError(() =&amp;gt; of([]))
    ).subscribe(results =&amp;gt; this.results = results);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;&lt;strong&gt;3. Template with Async Pipe (Alternative)&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;{{ results }}
{{ item }}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Top RxJS Operators for Angular Devs&lt;/strong&gt;&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Angular Use Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code inline=&quot;&quot;&gt;switchMap&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Cancel previous HTTP requests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code inline=&quot;&quot;&gt;catchError&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Recognize HTTP errors in services&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code inline=&quot;&quot;&gt;takeUntil&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Halt on component destruction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code inline=&quot;&quot;&gt;tap&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Debug or trigger side effects&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Example: Unsubscribe with &#39;takeUntil&#39;&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-typescript&quot;&gt;private destroy$ = new Subject();
ngOnInit() {
  this.dataService.getData().pipe(
    takeUntil(this.destroy$)
  ).subscribe();
}
ngOnDestroy() {
  this.destroy$.next();
  this.destroy$.complete();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;&lt;strong&gt;Angular + RxJS Best Practices&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Use the async Pipe:&lt;/strong&gt; Avoid manual unsubscriptions to prevent memory leaks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explicitly Unsubscribe:&lt;/strong&gt; Use takeUntil or Subscription arrays if async isn’t an option.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use HttpClient:&lt;/strong&gt; Return Observables directly from services.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never Nested Subscriptions:&lt;/strong&gt; Use switchMap and similar operators instead.&lt;/li&gt;&lt;/ol&gt;&lt;ul&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;strong&gt;Conclusion: Key Takeaways&lt;/strong&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;RxJS is Angular’s Async Superpower:&lt;/strong&gt; Use observables for HTTP, forms, and state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Operators Wisely:&lt;/strong&gt; switchMap, catchError, and takeUntil are vital.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Always Clean Up:&lt;/strong&gt; Use async or takeUntil to handle memory leaks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.instanceofjava.com/feeds/6601715666458674828/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.instanceofjava.com/2025/03/rxjs-in-angular-tutorial.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6601715666458674828'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2964959029992147971/posts/default/6601715666458674828'/><link rel='alternate' type='text/html' href='http://www.instanceofjava.com/2025/03/rxjs-in-angular-tutorial.html' title='RxJS in Angular tutorial'/><author><name>Instanceofjava</name><uri>http://www.blogger.com/profile/00477939351667667851</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>