Issue with ng-style not updating correctly

Within my html file, I have implemented the following code to showcase a square game board utilizing ng-style for dynamic updating of the background-color as the program progresses:

<div class="row" ng-repeat="col in board">
        <div class="column" ng-style="{'background-color': changeColor($parent.$index, $index)}" ng-repeat="row in col track by $index"></div>
</div>

The current implementation features a changeColor function that simply returns a color value. Moreover, the structure of the board variable is constructed like so:

function initializeBoard() {
    $scope.board=[];
    for (var i = 0; i < BOARD_SIZE; i++) {
        $scope.board[i] = [];
        for (var j = 0; j < BOARD_SIZE; j++) {
            $scope.board[i][j] = false;
        }
    }
}

$scope.changeColor = function(col, row) {
    return '#e6e6e6';
}

Despite these configurations, when attempting to execute the code, no visual output appears on the screen. The expected result was to render a square element.

Answer №1

Make sure to include content in your inner div that you are applying the ng-style to. If you don't add content or style it with width and height properties, it will not be visible.

Answer №2

It appears that the issue lies in the empty child div (second ng-repeat).

function MyController($scope) {
    var BOARD_SIZE = 10;

    function setUp() {
        $scope.board = [];
        for (var i = 0; i < BOARD_SIZE; i++) {
            $scope.board[i] = [];
            for (var j = 0; j < BOARD_SIZE; j++) {
                $scope.board[i][j] = false;
            }
        }
    }

    $scope.colour = function (col, row) {
        return '#e6e6e6';
    }

    setUp();

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="MyController">
    <div class="row" ng-repeat="col in board">
        <div class="column" ng-style="{'background-color': colour($parent.$index, $index)}" ng-repeat="row in col track by $index">{{row}}</div>
    </div>
</div>

If you do not plan to add any content, make sure to specify the height and width of the element (I used your column class).

function MyController($scope) {
    var BOARD_SIZE = 10;

    function setUp() {
        $scope.board = [];
        for (var i = 0; i < BOARD_SIZE; i++) {
            $scope.board[i] = [];
            for (var j = 0; j < BOARD_SIZE; j++) {
                $scope.board[i][j] = false;
            }
        }
    }

    $scope.colour = function (col, row) {
        return '#e6e6e6';
    }

    setUp();

}
.column {
    height: 20px;
    width: 60px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="MyController">
    <div class="row" ng-repeat="col in board">
        <div class="column" ng-style="{'background-color': colour($parent.$index, $index)}" ng-repeat="row in col track by $index"></div>
    </div>
</div>

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

The API response indicates that the service is currently not accessible

I'm attempting to retrieve a report from the MOZ API, but I keep receiving this response: { "status" : "503", "error_message" : "Service Temporarily Unavailable" } This is the code I am using: function MozCall(callback) { var mozCall = ' ...

Multiple minute delays are causing issues for the Cron server due to the use of setTimeout()

I have an active 'cron' server that is responsible for executing timed commands scheduled in the future. This server is dedicated solely to this task. On my personal laptop, everything runs smoothly and functions are executed on time. However, ...

What is the best way to input data into the verified full name box?

.html file executed code <input type="name" [(model)]="x.name" class="form-control" pattern="[a-z]" > Greetings to the members of Stack, I am in need of assistance. I am relatively new to Angular and I am looking for a way to validate the full nam ...

Having trouble with CSS :before pseudo-element and sibling selector?

Looking for a CSS solution to change the appearance of a triangle when the .hide class is activated within my markup. I attempted using a pseudo-element like so: summary:before { content: '\25BC'; } summary:before + .hide { con ...

Enforce a line break in flexbox and display items in the next column

Is there a way to break an item in a flexbox and display it at the beginning of the next column, based on the max height of the box? See the image below for reference. .flex{ display:flex; flex-direction:column; flex-wrap:wrap; max-height:200px ...

Why do my session details in the stripe webhook show as undefined?

During the development of my stripe checkout session, I encountered an issue where I can successfully send information to my webhook and print out all the session details, but I am unable to access individual pieces of information. Whenever I try to access ...

Access rejected due to X-Frame-Options: "http://test.test.net/Feedback/Create?appId=TestApp" prohibits cross-origin framing in MVC5

Currently, I am developing a website that is hosted on my company's internal network and can only be accessed from within the network. As a result, cross-domain requests are not a concern for me. As part of this website, I have included a "Provide Fe ...

Discover an Element within a JSON Array and Append a New Value to it

I've got an array of JSON data structured like this: [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Eve' } ] My goal is to locate an object by its id and append a ...

Overflow issue with floating labels in Bootstrap 5 within a grid container

Incorporated within this code snippet are floating selects placed inside a bootstrap grid: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4b6bbbba0a7a0a6b5a494e1fae7fae7">[emai ...

Boost Typography Size with Ionic

I've noticed this feature on multiple apps, but I'm having trouble understanding how to implement it. What I want is to have an icon for changing the font size (with + and - signs) so that when the user clicks on it, the font size either increase ...

The jQuery onchange function does not have the ability to limit the input to only

When using the script to sum the input value, everything seems to be working fine. However, I am facing an issue where clicking up and down on the "input#total_selfmark" does not change the value. Additionally, when I manually type in a number, the "input# ...

Synchronous communication using AngularJS

My goal is to execute a or b, or both, in AngularJS when we have c. The issue I am facing is that the calls are being made asynchronously, causing my data to become corrupted. if(a || c) { $http.get("path1"); } if(b || c) { $http.get("path2"); } ...

Tips for incorporating animation while altering element styles within JavaScript

I am looking to incorporate a sliding animation that moves the element downward when the display property is set to block, and upward when it's set to none or an empty string, here is the current JavaScript code I have: <script> function showQ ...

Is your Nodejs res.redirect function malfunctioning?

I'm encountering an issue with routing all HTTP traffic to HTTPS on my AngularJS website. Despite implementing the code below, the redirection is not working as expected whenever I try to access my site via HTTP. var express = require('express&a ...

Is it possible to leverage the flex features of react-native in a manner similar to the row/column system of

Is it a good idea to utilize flex in react native by creating custom components that retrieve flex values? I previously used the bootstrap grid system and now I am exploring react native. Is there a way to implement this example using react-native bootstr ...

How can controllers effectively communicate with each other in AngularJS?

What is the most effective method for inter-controller communication? Currently, my solution involves using window, but it feels like a messy workaround: function StockSubgroupCtrl($scope, $http) { $scope.subgroups = []; $scope.handleSubgroupsLoa ...

Exploration of search box with highlighted fields

Seeking assistance to enhance the highlighting feature after entering a letter in the search box. Here is the current code snippet: <!DOCTYPE html> <html> <head> <script type="text/javascript" src="javascripts/jquery-1.11.0.min.js"&g ...

Develop a fresh button using either JavaScript or PHP

I have designed a mini cart (drop down) on my WordPress site with different styles for when there are products and when empty. To enhance the design, I have utilized CSS pseudo elements before and after to add content dynamically. Is it possible to includ ...

Trouble with CSS solution for fixed header/columns in HTML table on Firefox

My current project involves implementing sticky headers/columns on a table for the client. Unfortunately, using a real script for this purpose is not feasible due to an already high object count on the page which is resulting in speed issues. However, I h ...

Protractor - Easily locating elements by targeting their children

I am attempting to modify the text of the input element, but I am having trouble identifying it. The element does not have an id or any other distinguishing characteristic. Additionally, this same object is repeated multiple times in the code, making it di ...