Listening to events in controllers to update the view in AngularJS

Within my application, I retrieve a list of objects from the server. These objects are then displayed on a left sidebar as a list and on a map (leaflet) as markers on the same page. The markers/map are rendered through a service, while the sidebar list is handled by a controller and simple view.

Whenever a user clicks on an item in the list or a marker on the map, I trigger a broadcast event from $rootScope.

Clicking on a sidebar item initiates the following code:

// in view
<div ng-click="markerClicked(object)">...</div>

// in controller
$scope.markerClicked = function(object) {
    $rootScope.$broadcast('markerClicked', object);
};

Clicking on a marker triggers this code:

angular.forEach(markers, function(object) {
    var _marker = L.marker([object.longitude, object.latitude],{
        clickable: true
    });

    _marker.on('click', function(e) {
        $rootScope.$broadcast('markerClicked', object)
    });

    _marker.addTo(service.map);
    service.layer.addLayer(_marker);
});

I handle this event within only one controller:

// I also tried $rootScope.$on()
$scope.$on('markerClicked', function(event, object) {
    $scope.object = object;
    $scope.hidden = false;
    console.log('event fires');
});

$scope.hidden serves as a flag for conditionally rendering the modal window (popup).

The issue arises when clicking on a marker - although 'event fires' is logged in the console and $scope.hidden is set to false, the modal window fails to appear. However, it works as expected when clicking on a sidebar item.

If anyone has encountered a similar problem, please advise on what might be going wrong.


I am utilizing webpack in the project, which may potentially be a factor affecting the functionality.

Answer №1

give this a shot

$scope.$on('markerSelected', function(event, item) {

    $scope.$apply(function(){
       $scope.item = item;
       $scope.isHidden = false;
    })
    console.log('event triggered');
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Does JSX have an API that can handle self-closing tags, such as <br>, if they are not closed properly?

For my latest project, I've been working on a ReactJS component using JSX and it looks something like this: <script type="text/jsx"> var HelloWorld = React.createClass({ render: function() { return ( < ...

Angular tree dropdown menu structure

Check out this functional jsfiddle: working example for the secondary level In the jsfiddle, I am able to assign a brand from the first dropdown and a model from the second dropdown to each car created through ng-repeat. It is all working smoothly. Howev ...

Is it possible to mimic a ref attribute with jest/rtl within a functional component?

I'm currently facing an issue with a functional component that includes a helper function. function Component() { imgRef = useRef(null) function helperFunction(node, ref) { if (!ref || !ref.current) return; ...do someth ...

Showing nested arrays in API data using Angular

I would like to display the data from this API { "results": [ { "name": "Luke Skywalker", "height": "172", "mass": "77", & ...

Function returning undefined when accessing prototype property in JavaScript

When attempting to create an image rotator using prototypal inheritance, I am encountering an error in the console showing: TypeError: this.curPhoto is undefined this.curPhoto.removeClass('previous'); I have placed this code in the callb ...

Utilizing component data in Vue.js to configure the router-link attribute

Is there a way to pass component data in <router-link to="...">? Here's an example: var data = { files: [{ name: "test", path: "/test" }] }; var component = { data: function() { return data; ...

NextJS-created calendar does not begin on the correct day

I'm facing an issue with my calendar code where it starts rendering on a Wednesday instead of a Monday. I want to adjust the layout so that it always begins on a Monday by adding some empty boxes at the start of the calendar. Essentially, I need to s ...

Controller function failing to trigger

I'm new to asking questions, so if I've made a mistake, please let me know. I've searched for an answer here but couldn't find one. Any help would be greatly appreciated. I'm attempting to load a list of "Countries" using a contro ...

What is the best way to set a default button in Angular?

I am working with a group of buttons in Angular Material code, and I would like to set the first button as the default option. When this button is clicked, it should display the content related to that button on the right side. https://i.sstatic.net/BFBma ...

the cached token retrieved by adal is consistently empty

To retrieve a cached token, I am utilizing the react-adal api import { authContext, } from '../auth' const AvatarContainer = () => { getPersonPhoto(authContext) return ( <Avatar /> ) } async function getPersonPhoto(au ...

Display a singular value using ng-show when identical data is received for the specified condition

This section will display the result based on the selected language. For example, if "English" is selected in myAngApp1.value, it will display the content in English. However, since all four languages have an English value in SharePoint list, it displays t ...

"Incorporate keyframes to create a mouseleave event with a distinctive reverse fade

After posing a similar question a few days back, I find myself encountering yet another hurdle in my project. The task at hand is to switch the background image when hovering over a button with a fade-in effect using @keyframes. However, I'm stuck bec ...

Insert some text into the div element. Create a new line

I'm looking to make a specific text on my webpage trigger a new line when displayed in a div. I've been struggling to figure out how to accomplish this: var original= "the text @n to change"; var changed = original.replace(/@n /g, '\ ...

Looking for assistance with jqGrid

Is there a method to confirm if the grid has finished loading using a separate function? My goal is to automatically click on the first row and move that row to the second grid once this action occurs. jQuery("#search_grid").jqGrid('setGridParam&apo ...

AngularJS dynamic resources allow for web applications to access and

I have created a directive for a search form that provides a template with a <form> tag, automated messages such as "No results found," and an optional suggestion message like "You can search by name or id." However, I am facing an issue where I nee ...

The Angular CLI encountered a 404 Not Found error while attempting to load the symbol-observable at http://localhost:4200

Issue encountered after upgrading angular cli to beta 9: Failed to load resource: the server responded with a status of 404 (Not Found) zone.js:461 Unhandled Promise rejection: (SystemJS) Error: XHR error (404 Not Found) loading http://localhost:4200/symb ...

Unexpected issue with PHP/Ajax/JQuery response functionality

I am experiencing an issue with my index.php file. Here is the code: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script src="ajax.js"></script ...

What could be the reason for the child element not occupying the full height of its parent container?

* { padding: 0; margin: 0; box-sizing: border-box; } body { margin: 50px; } .navbar { display: flex; align-items: center; justify-content: center; color: darkgreen; font-family: 'Vollkorn', serif; font-size: 1.2rem; font-w ...

Endless time continuum within the scheduling application

Looking for a way to make the time axis in my scheduling app infinite? Currently, it has a fixed length, but I want users to be able to scroll endlessly into the past or future. Any suggestions on how to achieve this? Check out this JSBin for a basic exam ...

Having trouble aligning a div in the middle of a slick-slider item using flex styling

I've created a slick slider component in Vue, and I'm facing an issue with centering a circular div inside each of the slider items. Despite trying to align and justify center along with adding margin:auto for horizontal centering, I can't s ...