Posts

Observables

Q1: What is the difference between an observable and a promise? =>The key differences are as follows: 1)Observable does not start untill subscription/subscribed, while Promises are executed immediately. 2)Observable provide many values while Promises provide one. 3)Observable subscription are cancellable while promise are not cancellable. 4)Observable provide operators to transform data. We use pipe to chain operators and only subs We should use promises over Observables when we need single subcription on single event. Q2: What is the difference between an observable and a subject? =>Observable can unicast while subject is a multicast observable. It means Subject allows values to be multicasted to multiple observables. Q3: What are some of the angular apis that are using observables? =>http, EventEmitter, async pipe etc Q4: How would you cache an observable data? =>We can use create a service implementing Replay subject. A replay subject can record given no of values and th...

Angular Forms

There are two types of forms: 1) Reactive forms: scalable, reusable and testtable 2)Template driven forms: simple Data flow in Reactive forms: -As user types value in input, input event is emitted with latest value by input element. -The control value accessor(links FormControl instances and native DOM elements) sends new value to FormControl instance. like: favouriteColorControl.setValue('red'), Here favouriteColorControl is control value accessor. -FormControl instance emits new values through valueChanges observable. -Any subscriber to valueChanges observable receives the new value. Data flow in Template driven forms: -All the steps are same as above -The control value accessor also calls the NgModel.viewToModelUpdate() method which emits an ngModelChange event. -The favoriteColor property in the component is updated to the value emitted by the ngModelChange event Differences: -Reactive forms are created in component class while template driven forms are created by directive...

Angular Components

Components define views, which are sets of screen elements that Angular can choose among and modify according to your program logic and data. Every application has at least one root component that connect a component hierarchy with DOM. Each component defines a class that contains application data and logic, and is associated with an HTML template that defines a view to be displayed in a target environment. The @Component() decorator identifies the class immediately below it as a component, and provides the template and related component-specific metadata. Decorators are functions that modify JavaScript classes. There are following ways for cross-component communication : using query params if there is parent and child then better to use @Input(), @Output(), EventEmitter() pass data through service using Observable. [Refer]- https://www.digitalocean.com/community/tutorials/angular-component-communication There are number of ways for parent child communication: [Refer...