ng-class expressions are constantly evolving and adapting

Within a div, I am utilizing

ng-class="{minifyClass: minifyCheck}"
.

In the controller, I monitor changes in two parameters - $window.outerHeight and

$('#eventModal > .modal-dialog').height()
. If there are any changes, I trigger the minifyChange() function to execute certain tasks.

function minifyChange(){
    if( $('#eventModal > .modal-dialog').height()+75 < $window.outerHeight ){
        $scope.minifyCheck = true;
    }else{
        $scope.minifyCheck = false;
    }
    //$scope.$digest();
}


$scope.$watch(function () {
   return $('#eventModal > .modal-dialog').height()
},function(){ return minifyChange() }, true)

$scope.$watch(function () {
   return $window.outerHeight;
},function(){ return minifyChange() }, true)

If I leave out the $scope.$digest(), I must "wait" until the next $scope.$digest() or $scope.$apply() occurs for the addition or removal of minifyClass based on the value of minifyCheck. This waiting period can lead to suboptimal user experience.

However, omitting the $scope.$digest() will result in an error indicating that the $digest is already in progress:

Error: [$rootScope:inprog] http://docs.angularjs.org/error/$rootScope:inprog?p0=$digest

My Angular code is intricate, making it challenging to identify the ongoing digest. Here is the complete error message:

Error: [$rootScope:inprog] http://errors.angularjs.org/undefined/$rootScope/inprog?p0=%24digest
    at Error (<anonymous>)
    at http://localhost:8000/static/angular/angular.min.js:6:453
    at h (http://localhost:8000/static/angular/angular.min.js:93:109)
    at g.$digest (http://localhost:8000/static/angular/angular.min.js:96:74)
    at minifyChange (http://localhost:8000/e=52a1a39345dc83b057c17dc1#v1:444:12)
    at Object.fn (http://localhost:8000/e=52a1a39345dc83b057c17dc1#v1:450:22)
    at g.$digest (http://localhost:8000/static/angular/angular.min.js:96:367)
    at g.$apply (http://localhost:8000/static/angular/angular.min.js:99:100)
    at http://localhost:8000/static/angular/angular.min.js:17:320
    at Object.d [as invoke] (http://localhost:8000/static/angular/angular.min.js:30:21) angular.js:8058
(anonymous function) angular.js:8058
(anonymous function) angular.js:5730
g.$digest angular.js:9096
g.$apply angular.js:9363
(anonymous function) angular.js:1127
d angular.js:3146
Sb.c angular.js:1125
Sb angular.js:1140
Mc angular.js:1091
(anonymous function) angular.js:17902
l jquery-2.0.3.js:2913
c.fireWith jquery-2.0.3.js:3025
x.extend.ready jquery-2.0.3.js:398
S jquery-2.0.3.js:398

My query: Is there a way to update the class without triggering this error? Are there alternative Angular functions that could serve this purpose? Alternatively, if avoiding the use of $digest is impossible, how can I pinpoint and debug the concurrent digest process?

Answer №1

It's always advisable to delegate DOM access tasks to directives rather than handling them in your controller. One effective approach is as follows:

Develop a directive that monitors element.height() and $window.height(). (You can refer to an example here). Additionally, check out How (not) to watch element size changes

Each time the watch function activates, calculate the minifyCheck value. Hopefully, this guidance proves helpful.

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

What causes functions operating on mapped objects with computed keys to not correctly infer types?

If you are seeking a way to convert the keys of one object, represented as string literals, into slightly modified keys for another expected object in Typescript using template string literals, then I can help. In my version 4.9.5 implementation, I also ma ...

Why does using rgba color with an alpha value cause my div to become see-through?

I am currently attempting to assign a background color to a specific style of pop-up, but whenever I use a value that includes an alpha channel, it ends up being transparent. This is the CSS code I have been using: --color: 255, 144, 106, 0.18; background ...

A guide on selectively removing a value from a javascript object when calling setState in ReactJS

updateDishDetails(id, quantity) { if (quantity !== 0) { this.setState( prevState => ({ bookingFormData: { ...prevState.bookingFormData, dishDetails: { ...prevState.bookingFormData.dishDe ...

jQuery animation not executing as expected due to transition issues

I'm looking to create a cool effect where the previous quote disappears and a new one appears when I click on a button, all with a smooth transition effect. In my attempt below using jQuery .animate and opacity property, I'm struggling to make i ...

Changing dimensions of cube on stable base

I'm currently working on a project involving a dynamic cube that can be scaled in real time by adjusting its mesh. However, I'm facing a challenge in keeping the cube fixed to the floor while changing its height. Here's a snippet of the code ...

Adding an object to an ArrayList: A step-by-step guide

I'm encountering issues with adding objects to the list. I have a list of floors, each floor containing rooms. I can successfully add a floor, but I'm unsure how to add rooms to the floor list. I've attempted to access floor[index] or id, b ...

Displaying and concealing a div based on the scroll position

I have implemented a script that hides a div and then shows it when I scroll past a certain point on the page. It is working correctly, but once I scroll back up to the top, the div remains visible. Can someone suggest a modification to the code that will ...

Retrieve data from the table and dropdown menu by clicking a button

A script is in place that retrieves data from two columns (Members, Description) dynamically from the table upon button click. Table html Here is the JQuery code responsible for extracting values from the table: $(function() { $('#myButton') ...

Access the modal by simply clicking on the provided link

I have implemented a code snippet below to display data from MySQL in a modal popup. Everything is functioning correctly, but I am interested in triggering the modal by clicking a link instead of a button. Is it feasible to achieve this considering I have ...

Guide on inserting a MUI datepicker string value into the object state

Currently, I am in the process of developing a todo-list application that includes fields for description, priority, and date. To capture the priority and description inputs, I utilize TextFields which trigger the {inputChanged} function. Within this funct ...

Executing a function every time a prop is updated within the component

I have a prop named transcript in one of my components. Whenever I speak a voice intent, it gets updated. I want to execute a function every time the transcript changes and pass the transcript as an argument. In this code snippet, I attempted to use an On ...

Exploring the concepts of arc functions in discord/js programming

Example: Having trouble understanding how to create an arc in JavaScript based on a percentage input? Here's a simple explanation: feed it a percentage and it will create an arc that represents that percentage of a circle. I've managed to get t ...

Azure App Service for Mobile and Web applications

Currently, I am in the initial stages of assessing and designing the potential architecture for a Mobile App (using Xamarin) and a Web App (most likely using Angular). The Azure App Service appears to be a suitable option for hosting these services. Howeve ...

Working with Variables in JavaScript and PHP Scripting Languages

I have a PHP script that displays all the folders (categories) on my website: echo "<ul>"; foreach(glob('category/*', GLOB_ONLYDIR) as $dir) { $dir = str_replace('category/', '', $dir); echo '& ...

Client-side filtering of jqGrid with multiple conditions

I am faced with a challenge in filtering records in a jqGrid based on multiple conditions. For example, if there are columns for Name, Age, and City, and I need to filter the grid using the following condition: Name = "Mark" and Age = 25 and City = &apos ...

Using Javascript to Assign Value to Variables

I have been working on this code snippet: http://jsfiddle.net/9B84H/26/ function autosuggest() { var input = document.getElementById('location'); var options = {types: [],}; var autocomplete = new google.maps.places.Autocomplete(input, o ...

How to use jQuery to hide list items after a certain threshold

$('li[data-number=4]').after().hide(); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li data-number="0"">1</li> <li data-number="1">2</li> ...

What is the best way to align an InputAdornment IconButton with an OutlinedInput in Material-UI?

Struggling to replicate a text input from a mockup using Material-UI components. Initially tried rendering a button next to the input, but it didn't match. Moving on to InputAdornments, getting closer but can't get the button flush with the input ...

responsive cascading dropdown.. preassigned value

I've come across this interesting script on a website and it's been working pretty well for me so far. The script can be found at the following link: . However, I have a specific requirement when it comes to setting a certain value for the catego ...

Exploring the Dynamic Styling Power of Angular's ng-class Directive Alongside the Transition from

I recently updated my app from Fontawesome version 4 to 5 by following the guidelines provided HERE. Everything looks great and appears to be working smoothly, except for the dynamic icons... In my Angular-based app, the icon to display is often dynamic: ...