Updating ng-init in AngularJS triggered by a changeOR Changing

Is there a way for ng-init to be updated when the tableCells change? I'm looking for a method that will avoid angular from calling findCellsByDate twice.

I've also attempted changing ng-init to ng-bind or ng-model, but ng-bind displays [object Object] and ng-model throws an error because there is no attribute.

<span ng-init='cells = findCellsByDate(tableCells, day)'>
    <div class='text' ng-show='forecastAndActualForCellsAreNotEmpty(cells)'>
        <span ng-bind='getTotalHoursPercentageForCells(cells)'></span>
        <span>%</span>
    </div>
</span>

Answer №1

It seems like ng-init may not function in the way you expect. Instead, consider modifying some of your controller or model code to include a boolean variable for

forecastAndActualForCellsAreNotEmpty
that can be updated accordingly.

Alternatively, you could achieve this using CSS. If

forecastAndActualForCellsAreNotEmpty
returns false, then getTotalHoursPercentageForCells may return 0. You could bind this value to a data attribute and apply a CSS rule to hide the div element. However, doing this without utilizing ng-init would require taking control of the content using CSS as well, possibly through the use of :before and :after pseudo-elements.

<div class='text' data-percentage='{{getTotalHoursPercentageForCells(findCellsByDate(tableCells, day))}}'>
    </div>

Corresponding CSS:

div[data-percentage=0]{
    display: none;
}

div[data-percentage]:before{
    content: attr(data-percentage) "%";
    display: block;
}

While using :before or :after for displaying application content is not recommended, it can work for stylistic elements. Ultimately, I suggest considering adjustments to the model structure as the preferred solution.

Answer №2

To ensure that the cells variable is updated when changes occur in the tableCells variable, implement a watch function within your controller.

$scope.$watch('tableCells', function(tableCells) {
    if(!tableCells) return;

    $scope.cells = findCellsByDate(tableCells, $scope.day);        
});

This approach maintains the logic within the controller for better organization.

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

Applying the variables as elements within a foreach iteration

I'm currently developing a learning management system and encountering an issue with my code. foreach($datas as $data){ ?> <div style="background-color: #3B4FDF; border-radius: 3%; width: 30%; height: 220px; margin-right: 5%; cursor: poi ...

Tips on returning the array

I've been struggling to figure out how to extract the 'sheetsArray' from the code snippet below so that I can use it for various tasks. Despite my efforts and hours of searching online, I haven't been able to find a solution. It seems l ...

Button for Processing PayPal Payments

Is there a security risk in using a text button to submit a payment form in PayPal Payments Standard instead of an image button like this: <input type="image" src="http://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt=""> ...

How can we have a div stay at the top of the screen when scrolling down, but return to its original position when scrolling back up?

By utilizing this code, a div with position: absolute (top: 46px) on a page will become fixed to the top of the page (top: 0px) once the user scrolls to a certain point (the distance from the div to the top of the page). $(window).scroll(function (e) { ...

Exploring various sub-arrays within Firebase

My title doesn't quite capture the issue I'm facing. I've been delving into firebase and angularfire, learning how to work with data manipulation. Everything was going smoothly until I encountered this peculiar array structure. https://i. ...

Tips for displaying only one image when clicked and hiding other divs:

Currently tackling a project that involves JavaScript and I've hit a roadblock. Before you dive into the text, take a look at the image. Check out the picture here. I have successfully created a slider, but now I want to implement a feature where cli ...

The functionality of Jquery draggable is not functioning

  On one of my websites, I have a div that contains an image that is wider than the div itself. To view the entire image, it needs to be moved using the mouse. However, I have encountered a problem when moving the image to the right and in other directi ...

When trying to load AJAX, it does not function properly unless the page is refreshed

I'm currently working on a web page and encountering some difficulties. Within my HTML, I have two divs that are refreshed using AJAX. The issue is that the content loading seems to be inconsistent unless I manually refresh the page or implement a tim ...

What is the solution to fixing a flashing div?

I've been using a jQuery method to make my div blink. Check it out: JSFIDDLE <div class="blink">blinking text</div>non-blinking <div class="blink">more blinking text</div> function blink(selector){ $(selector).fadeOut(& ...

Struggling with JQuery to revert an element back to its original position after the mouseout event

Hello all, I'm a newcomer here and I've been trying my hand at some basic JQuery. However, I've encountered an issue that I could use some help with. Have you ever come across those boxes on websites where when you hover over them, an arrow ...

Struggling to access the html elements within a component once the ng2 page has finished loading?

I am working on a web app that utilizes ng2-smart-table and I want to hide all cells within the table. However, when attempting to retrieve all the cells using document.getElementsByTagName("td") in the ngAfterViewInit() lifecycle hook, I noticed that the ...

What is the best way to automatically create a PDF file that includes interactive JavaScript charts?

My goal is to create a word document or PDF containing charts from various JavaScript chart libraries. I've successfully implemented these libraries on websites, but now I want to include them in my documents as well. My initial attempt involved usin ...

Error: The function setDarkTheme is not defined

Every time I click on the Dark button to apply dark theme on my page, I encounter an error. It seems like the error lies in the onClick function of the button and I can't seem to figure it out. Error message App.js import { useState } from 'rea ...

Leveraging NPM with Laravel 5 for AngularJS Integration

I am eager to integrate AngularJS with Laravel 5. Since Laravel relies on NPM for installing packages like gulp and laravel-elixir, my initial step was to modify the packages.json file: { "private": true, "devDependencies": { "gulp": "^3.8.8", ...

Create a function within jQuery that triggers when an option in a select field is chosen

As I edit a podcast, I have encountered a situation where an option is being selected through PHP code. Now, I am looking to implement jQuery functionality for when the option is selected from the select field. I have searched through various questions an ...

jQuery UI Drag and Drop problem with mouse positioning

I am currently working on a website that allows users to drag different "modules" (squares with information) from one location to another on the page using jQuery UI. However, I am facing an issue where when a module is dragged to a droppable zone, the sc ...

Angular 1.5.0 - What is the reason for factory being invoked only once?

I am facing an issue with my html template where the data from an Angular factory is only loaded once when the page initially loads. Subsequent page openings show the same results from the first backend call, indicating a potential caching issue within the ...

How can I switch the values of two select components in React js when clicked?

I am working on a project where I have two select components positioned on the right and left side respectively, each with different values - A on the right side and B on the left side. Now, in response to a button click event, I need to swap component A t ...

Modify the value of the <select> tag based on whether it is required or not

When utilizing a my-select.component within a form component, the following code snippet is used: <div *ngIf="items?.length"> <select [ngModel]="selectedItem" (ngModelChange)="valueChanged($event)"> <optio ...

Why is it that this specific ASP.NET + jQuery + JavaScript custom MVC setup isn't displaying any content?

After attempting to incorporate this JavaScript MVC example from into an ASP.NET page, I am facing issues as a beginner with jQuery. Nothing seems to show up on the page, and I'm unsure why. Update: I have included the missing HTML listbox, but now ...