Saturday 18 February 2023

Example that demonstrates how "switchMap" works:

"SwitchMap " working :


Here are the perfect example to understand working of "switchmap"

Suppose we have an input field that users can type into to search for books in a library. As the user types, we want to make an HTTP request to a server to get book suggestions based on the input. However, we don't want to make a new request for every keystroke, as that would be inefficient. Instead, we want to wait for the user to pause typing for a certain amount of time before making the request.

import statement for angular

import { fromEvent } from 'rxjs'; import { switchMap, debounceTime, distinctUntilChanged } from 'rxjs/operators'; import { ajax } from 'rxjs/ajax';
/* Get data from the search box */ const searchBox = document.getElementById('search-box');
const bookSuggestions$ = fromEvent(searchBox, 'input').pipe( debounceTime(500), // wait 500ms after each keystroke distinctUntilChanged(), // only emit if the value has changed switchMap((event) => { const searchQuery = (event.target as HTMLInputElement).value; return ajax.getJSON(`https://mylibrary.com/books?q=${searchQuery}`); }) ); bookSuggestions$.subscribe((books) => console.log(books));


In this example we first import all the necessary module in angular to achieve functionality of
switchmap. we first create an Observable from the input event of the search box using the fromEvent function. We then use debounceTime(500) to wait for 500ms after each keystroke before emitting the event. This ensures that we don't make a new request for every keystroke. We also use distinctUntilChanged() to only emit events if the search query has changed.

Finally, we use switchMap to transform the emitted events into an Observable that makes an HTTP request to the server using the ajax function. If a new event is emitted while the previous request is still in progress, switchMap will unsubscribe from the previous request and make a new one based on the new event. This ensures that we always get the most up-to-date search results.

Friday 17 February 2023

An Introduction to JavaScript: The Language That Powers the Web

 Introduction:

JavaScript is one of the most popular programming languages in the world, and for good reason. It is the primary language used to create interactive web pages and dynamic user interfaces. In this blog post, we will give you a brief introduction to JavaScript and explain why it is such an important language for web development.

Section 1: What is JavaScript? JavaScript is a high-level, object-oriented programming language that is primarily used to create dynamic, interactive web pages. It was first created in 1995 by Brendan Eich and is now one of the three core technologies of the World Wide Web, along with HTML and CSS.

Section 2: Why is JavaScript important? JavaScript is important because it allows web developers to create interactive and dynamic websites. With JavaScript, you can create pop-ups, animations, interactive forms, and much more. It is also widely used on the server-side, using frameworks like Node.js, allowing developers to create full-stack web applications.

Section 3: What can you do with JavaScript? JavaScript is a versatile language that can be used for a wide range of tasks. Here are just a few examples:

  • Create interactive user interfaces: JavaScript can be used to create dynamic user interfaces that respond to user input and interactions.
  • Validate user input: JavaScript can be used to validate user input in forms, ensuring that data is entered correctly.
  • Create animations: With JavaScript, you can create animations and visual effects that add an extra layer of interactivity to your web pages.
  • Make API calls: JavaScript can be used to make API calls to retrieve data from servers and display it on a web page.
  • Build web applications: Using frameworks like React, Vue, or Angular, you can use JavaScript to build powerful web applications that run on the client and server side.

Section 4: How do you learn JavaScript? Learning JavaScript is relatively easy, especially if you already have some experience with programming. There are many resources available, including online courses, tutorials, and books. Some popular online resources for learning JavaScript include Codecademy, FreeCodeCamp, and W3Schools.

Conclusion: JavaScript is an essential language for web development, allowing developers to create dynamic and interactive web pages. With the rise of full-stack web development, JavaScript has become even more important, as it is now used on both the client and server sides. If you are interested in web development, learning JavaScript is a great place to start.

Thursday 16 February 2023

javascript to find any element in an XML document that contains the text content 'Hello'

 How we can use javascript to find any element in an XML  document that contains the text content 'Hello';



with this example first we have to create xml document into a variable like this 

const parser=new DOMParser();

const xmlDoc=parser.parseFromString(text, 'text/xml')

// Get all elements in the XML document
const elements = xmlDoc.getElementsByTagName('*');

// Iterate over the elements and find any that contain text content "Hello"
for (let i = 0; i < elements.length; i++) {
  const element = elements[i];
  if (element.textContent === 'Hello') {
    console.log(`Found matching element with tag name "${element.tagName}"`);
  }
}


Above javascript code first retrieve all elements in XML document using the 'getElementsByTagName'
 method. It then iterates over the elements and check the each element's `textContent ` property to see if it matches the string 'Hello'. If it does, the code prints the tag name of the matching elements to console.

this code should work with any valid XML document.


Example of raw XML document: 


<?xml version="1.0" encoding="UTF-8"?>
<root>
  <greeting>Hello</greeting>
  <message>How are you?</message>
</root>

Saturday 12 November 2022

Concise way of declaring constructor in C#

 The Concise way of declaring a constructor in C# is also called the Expression bodied  Constructor 


In the previous, we are declaring a constructor like this 


public class Person 

{

public string Name {get;}

public int Age{get;}

public Person(string name, int age){

Name=name;

Age=age;

}

}

But now we can declare the more concise way of declaring a constructor in C#

Public class Person

{

public string Name{get;}

public int Age{get;}

public Person(string name, int age)=>(Name,Age)={name,age}

}

Sunday 21 June 2020

Create a component without spec file in Angular 9

How to Create a component without spec file in Angular 9?


In the angular 9 there are two ways to do this.

1. By using angular CLI command

Just use following commands to remove spec file for generate component

ng generate component componentname --spec=false;


2. or using Angular.json

just use following command

ng config schematics.@schematics/angular:component.spec false


it will create following section in angular.json

AdSense

The Ultimate Guide to Interceptors: Understanding Their Power and Functionality

  The Ultimate Guide to Interceptors: Understanding Their Power and Functionality An interceptor is a service that can intercept HTTP reques...

Follow