Rambling thoughts on GIS and Remote sensing tools, Data visualization, Geospatial application development, GIS programming/scripts and spatial technologies....
Tutorial: Sharing Data Between Angular Components - Parent to Child, Child to Parent, and between siblings
Get link
Facebook
X
Pinterest
Email
Other Apps
Great video talking about sharing Data Between Angular Components - Parent to Child, Child to Parent, and between siblings by Jeff Delaney, AngularFirebase.
(COPIED HERE FOR MY OWN FUTURE REFERENCE)
Data sharing is an essential concept to understand before diving into your first Angular project. In this lesson, I provide four different methods for sharing data between Angular components.
The Parent-Child-Sibling structure of our Angular app.
Parent to Child: Sharing Data via Input()
This is probably the most common and straightforward method of sharing data. It works by using the Input() decorator to allow data to be passed via the template.
ViewChild allows a one component to be injected into another, giving the parent access to its attributes and functions. One caveat, however, is that child won’t be available until after the view has been initialized. This means we need to implement the AfterViewInit lifecycle hook to receive the data from the child.
Child to Parent: Sharing Data via Output() and EventEmitter
Another way to share data is to emit data from the child, which can be listed to by the parent. This approach is ideal when you want to share data changes that occur on things like button clicks, form entires, and other user events.
In the parent, we create a function to receive the message and set it equal to the message variable.
In the child, we declare a messageEvent variable with the Output decorator and set it equal to a new event emitter. Then we create a function named sendMessage that calls emit on this event with the message we want to send. Lastly, we create a button to trigger this function.
The parent can now subscribe to this messageEvent that’s outputted by the child component, then run the receive message function whenever this event occurs.
@Output() messageEvent = new EventEmitter<string>();
constructor() { }
sendMessage() {
this.messageEvent.emit(this.message)
}
}
Unrelated Components: Sharing Data with a Service
When passing data between components that lack a direct connection, such as siblings, grandchildren, etc, you should you a shared service. When you have data that should aways been in sync, I find the RxJS BehaviorSubject very useful in this situation.
You can also use a regular RxJS Subject for sharing data via the service, but here’s why I prefer a BehaviorSubject.
It will always return the current value on subscription - there is no need to call onnext
It has a getValue() function to extract the last value as raw data.
It ensures that the component always receives the most recent data.
In the service, we create a private BehaviorSubject that will hold the current value of the message. We define a currentMessage variable handle this data stream as an observable that will be used by the components. Lastly, we create function that calls next on the BehaviorSubject to change its value.
The parent, child, and sibling components all receive the same treatment. We inject the DataService in the constructor, then subscribe to the currentMessage observable and set its value equal to the message variable.
Now if we create a function in any one of these components that changes the value of the message. when this function is executed the new data it’s automatically broadcast to all other components.
In my custom angular web application, I had to pull the data from different ArcGIS server environments, say Dev ArcGIS server and Test ArcGIS server environment, some of my items are directly referred from ArcGIS server services and some of them are from Enterprise portal referring the WebMapId and PortalId from both environment (Dev, Test). Servers are in federated environment. To pull the data from different ArcGIS server environment, user must login in each environment. In addition, user must login to get inside the custom application and Enterprise AD Group was set to authenticate users on the custom web application and ArcGIS server environments. So, there will be 3 login attempts (1 –application itself, 2- Dev server/portal, 3- Test server/portal) to use the application, which doesn’t provide good user experience. To improve the better application workflow, I decided to reduce the number of logins required in the application and use AD Username and Password captured du...
When migrating the Visual Studio projects from .Net Framework 4.8 to .Net 5, you may encounter in the following error regarding Interaction.Triggers . Error XDG0008 The name "Interaction" does not exist in the namespace "http://schemas.microsoft.com/xaml/behaviors". The one solution to fix it is to install “ Microsoft.Xaml.Behaviors.Wpf ” from Nuget And update the namespace ( if needed) xmlns : i ="http://schemas.microsoft.com/expression/2010/interactivity" to xmlns:i ="http://schemas.microsoft.com/xaml/behaviors"
I had mistakenly save my class as fileName.text file and tried to rename it with fileName .java but the IntelliJ couldn't recognize it and throws the error saying.. After spending long hours on internet, I found a solution and sharing/keeping here for future reference. Step 1: Click "File"==> "Settings" Step 2: Expand "Editor" & Click "File Types" Step 3: You will see all file types on Right. Navigate to the "Text Files" and Click it Step 4: You should able to see your file name on the bottom of Registered Patterns (lower box) Step 5: Remove your file from the Registered Patterns. The problem should solved and let you to rename with fileName.java Step 6: If not, delete the file from the project and create it again with name fileName
Comments
Post a Comment