Establish the directive upon receiving the broadcast

Is there a way to trigger a directive when a specific event occurs on the backend and sets a value to false?...

This is where the event is being captured


    .factory('AuthInterceptor', function($q, $injector, $rootScope) {
        return {
            response: function(response) {
                if (response.data.lineStatus) {
                    $rootScope.$broadcast('line:lineStatus', response.data.lineStatus);
                }
                return response;
            }
        });

Then, I handle the broadcast in this section


    .factory('BetSlipFactory', function($rootScope) {
        var processingLineMoves = false;

        $rootScope.$on('line:lineStatus', function(ev, status) {
            status.selections = _.map(status.selections, function(selection) {
                selection.display = false;
                return selection;
            });
            if (!status.linesOK) {
                if (!processingLineMoves) {
                    processingLineMoves = true;
                    $rootScope.linesMovedModal = $popover(angular.element('#linesMovedModal'), {
                        template: 'views/linesMovedModal.html',
                        html: true,
                        autoClose: false,
                        placement: 'left',
                        trigger: 'manual',
                        animation: 'fx-fade-down',
                        scope: _.assign($rootScope.$new(), status, {
                            okPicks: function(selection, selections, index) {
                                if (selections[index + 1] || selections.length > 1) {
                                    $rootScope.betLoader = true;
                                    selections.splice(index, 1);
                                    $rootScope.betLoader = false;
                                } else {
                                    $rootScope.linesMovedModal.hide();
                                    processingLineMoves = false;
                                    selections.splice(index, 1);
                                }
                            },
                            removePick: function(selection, selections, index) {
                                console.log('selections', selections);
                                betSlipSelectionRequest('/betSlip/removeSelection', {
                                    game: selection.game,
                                    pair: selection.pair,
                                    line: selection.line
                                }).then(function() {
                                    selections.splice(index, 1);
                                    $rootScope.$broadcast('lines:removeAllLines');
                                    if (selections[index + 1] || selections.length > 1) {
                                        $rootScope.betLoader = true;
                                        $rootScope.betLoader = false;
                                    } else {
                                        $rootScope.linesMovedModal.hide();
                                        processingLineMoves = false;
                                    }
                                }, function(err) {
                                    console.log(err);
                                });
                            }
                        })
                    });
                    $timeout(function() {
                        $rootScope.linesMovedModal.show();
                    }, 800);
                }
            }
        });

    });

Essentially, the $rootScope.$on part needs to be integrated into a directive. The purpose is to activate a popover that shows some information, with the starting point being the $rootScope variable: $rootScope.linesMovedModal

The reason for transitioning this to a directive is due to the usage of DOM manipulation using

$popover(angular.element('#linesMovedModal')

In case you are interested, here is the link to the popover component I am utilizing: Popover Component

Answer â„–1

.directive('customDirective', function($rootScope, $popover, $timeout, BetSlipFactory) {
  return {
    restrict: 'E',
    link: function(scope, element, attrs) {
      var processingMoves = false;
      scope.$on('line:lineStatus', function(ev, status) {
        status.selections = _.map(status.selections, function(selection) {
          selection.display = false;
          return selection;
        });
        if (!status.linesOK) {
          if (!processingMoves) {
            processingMoves = true;
            scope.customModal = $popover(element, {
              template: 'views/customModal.html',
              html: true,
              autoClose: false,
              placement: 'bottom',
              trigger: 'manual',
              animation: 'fx-fade-down',
              scope: _.assign(scope.$new(), status, {
                okayPicks: function(selection, selections, index) {
                  if (selections[index + 1] || selections.length > 1) {
                    scope.betLoader = true;
                    selections.splice(index, 1);
                    $timeout(function() {scope.betLoader = false;}, 100);
                  } else {
                    scope.customModal.hide();
                    processingMoves = false;
                    selections.splice(index, 1);
                  }
                },
                deletePick: function(selection, selections, index) {
                  CreateNewFunctionOnService(selection).then(function() {
                    $rootScope.$broadcast('lines:removeAllLines');
                    if (selections[index + 1] || selections.length > 1) {
                      selections.splice(index, 1);
                      $rootScope.betLoader = true;
                      $timeout(function() {scope.betLoader = false;}, 100);
                    }else {
                      $rootScope.customModal.hide();
                      processingMoves = false;
                      selections.splice(index, 1);
                    }
                  }, function(err) {
                    console.log(err);
                  });
                }
              })
            });
            $timeout(function() {
              scope.customModal.show();
            }, 200);
          }
        }
      });
    }
  };
});

Feel free to give it a try and provide feedback

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

Updating a Nested Form to Modify an Object

There is an API that fetches an object with the following structure: { "objectAttributes": [ { "id": "1", "Name": "First", "Comment": "First" }, { "id": "2", "Name": "Second", "Comment": "Second" } ] ...

What is the process of utilizing marked plugins within a Vue3 project?

I attempted to integrate the marked plugin into my Vue.js applications. After installing [email protected], I did not encounter any issues during compilation. However, when I viewed the contents in the browser, nothing appeared. My Vue project was built u ...

React-querybuilder experiencing issues with validator functionality

While utilizing the react-querybuilder, I have encountered an issue with field validation not functioning correctly. Upon reviewing this StackBlitz, it appears that when clicking on Rule and checking all fields, there are no errors present. export const fi ...

Obtain one option from the two types included in a TypeScript union type

I am working with a union type that consists of two interfaces, IUserInfosLogin and IUserInfosRegister. The TUserInfos type is defined as the union of these two interfaces. export interface IUserInfosLogin { usernameOrEmail: string; password: string; } ...

Seeking assistance with basic Javascript/Jquery for Ajax on Rails 3 - can anyone help?

I've been diving into JavaScript and hit a roadblock. At the moment, I have a very basic gallery/image application. My goal is to create a functionality where clicking on an image will lead the user to a URL stored in my model data using AJAX. Additi ...

What are the appropriate situations for utilizing getStaticPaths()?

Right now, an API call is being made in a main component and the fetched data is saved in a Singleton. This singleton data needs to be accessed by the getStaticPaths() function. However, due to the fact that getStaticPaths() pre-renders it, the singleton ...

Exploring OOP Strategies within the Angular MVC Framework!

After coming across a question on Stack Overflow that was similar to mine, I realized I have a lot to learn about Object-Oriented Programming (OOP). Up until now, my coding experience has been mainly procedural. Before diving into my question, let me provi ...

What is the correct way to implement ng-repeat with this JSON object?

I stumbled upon a "ng-repeat test fiddle" while browsing Google, and decided to update it by adding double quotes similar to the format of my json string: http://jsfiddle.net/tRxzr/602/ However, the version I created doesn't seem to be functioning pr ...

Accessing parent directives for forms - custom form names and dynamic validation

Introduction My current task involves validating dynamically created forms within a directive. These forms are all managed and submitted separately. When the form is submitted, I need to display validation errors for each individual form without any issu ...

Retrieve HTML content from a JSON object and render it on a web page

I am facing a challenge with decoding html that is in json format. I'm struggling to figure out how to retrieve my html and display it on a page. It seems like json_decode isn't the solution. Can anyone help me with this issue? Appreciate any as ...

Searching and selecting columns in React using Material-UI

Currently, I am using Material-UI data tables and have implemented a search functionality similar to this Codesandbox example, which only searches the Name/Food column. This is my existing code snippet: const [filterFn, setFilterFn] = useState({ fn: items ...

What is the best way to create a delay so that it only appears after 16 seconds have elapsed?

Is there a way to delay the appearance of the sliding box until 16 seconds have passed? <script type="text/javascript"> $(function() { $(window).scroll(function(){ var distanceTop = $('#last').offset().top - $(window).height(); ...

Looking for a way to update template variables in copy using grunt?

I am currently utilizing load-grunt-config along with grunt-contrib-copy. My objective is to have the copy task replace certain template tags using the 'process' option. I understand that replacing template tags is feasible based on the grunt-co ...

A peaceful WCF service initiates a client callback whenever a server update occurs

In the process of developing a WCF RESTFUL service on top of a c++ console application, I am confronted with an issue. The client accesses this c++ application through my restful wcf service using a browser. Every time there is an update received by my w ...

What is the method to determine the size of a file in Node.js?

Have you ever wondered how to accurately determine the size of a file uploaded by a user? Well, I have an app that can do just that. The code for this innovative tool is provided below: Here is the snippet from server.js: var express = require('expr ...

Experiencing issues utilizing vue.js to retrieve information from a REST API

Using vue.js, I am attempting to fetch data from a rest api but encountering issues. Unfortunately, the response data is not being displayed and no error status is shown either. It's puzzling trying to identify what may have gone wrong. Below is my i ...

JavaScript Object-Oriented Programming - Accessor method that retrieves a property from the parent

Having trouble with implementing getters and setters for model objects in Angular. Facing an error: TypeError: Cannot read property 'firstName' of undefined at User.firstName (http://run.plnkr.co/AvdF2lngjKB76oUe/app.js:35:32) The code snippet: ...

Incorporate the npm mqtt package into my Angular application

I'm trying to incorporate the package https://www.npmjs.com/package/mqtt#connect into my Angular project. Since it's not a standard Angular package, I'm unsure of what I need to include in the module and controller to make it function correc ...

Releasing Typescript 2.3 Modules on NPM for Integration with Angular 4

Although there are instructions available in Writing NPM modules in Typescript, they are outdated and there are numerous conflicting answers that may not be suitable for Angular. Additionally, Jason Aden has delivered an informative presentation on youtu ...

Utilizing jQuery for AJAX-Based Deletion of Records

Hey, I stumbled upon a code snippet that allows me to delete a row from my database using Ajax. However, there seems to be an issue where the table that should disappear when deleted is not working properly. Here is the JavaScript code: <script type=" ...