The issue with Angularjs not displaying updated model data in ng-repeat view

I've encountered an issue with a "menu directive" not updating the items in the html view when the model changes. I'm using AngularJS with a for-each statement. Here is the minimal code snippet:

The directive:

export function SideNavbarDirective() {
    'ngInject';

    let directive = {
        restrict: 'E',
        templateUrl: 'app/components/sideNavbar/sideNavbar.html',
        scope: {
        },
        controller: SideNavbarController,
        controllerAs: 'vm',
        bindToController: true
    };

    return directive;
}

class SideNavbarController {
    constructor($scope, $timeout) {
        'ngInject';

        $scope.myItems = [
            {
                displayName:"1",
                highlight:true,
            },
            {
                displayName: "2",
                highlight: false,
            },
            {
                displayName: "3",
                highlight: false,
            },
        ];

        $timeout(() => {
            $scope.myItems[0].highlight = false;
            $scope.myItems[1].highlight = true;
            $scope.myItems.length = 2;
            $scope.$apply()
        }, 4000);

    }
}

The HTML:

<div ng-repeat="item in myItems" layout="row">
    <div flex layout="column>
            <div layout="row">
                <span>{{item.displayName | translate}}</span><span flex=""></span><span ng-show="{{item.highlight}}">*</span>
            </div>
    </div>
</div>

Initial view:

1* 
2
3

After the timer:

1* 
2

Expected result after the timer:

1 
2*

When removing an item, it reflects changes. However, modifying the content of objects in the array goes unnoticed by Angular. Any insights on what could be wrong?

EDIT: I updated the HTML to display just the value of the highlight property instead.

<div ng-repeat="item in myItems" layout="row">
    <div flex layout="column">
            <div layout="row">
                <span>{{item.displayName | translate}}</span><span flex=""></span><span>{{item.highlight}}</span>
            </div>
    </div>
</div>

Now the output is: Initial view:

1 true
2 false
3 false

After the timer:

1 false
2 true

So now, the expected values are displayed. Confusion arises from the ng-show expression. Could that be the issue? Feeling more puzzled than ever.

Answer №1

There is no requirement for curly braces in ng-show; therefore, it should simply be written as ng-show="item.highlight".

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

VS Code sees JavaScript files as if they were Typescript

I have recently delved into the world of Typescript and have been using it for a few days now. Everything seems to be working smoothly - Emmet, linting, etc. However, I've encountered an issue when trying to open my older JavaScript projects in VS Cod ...

What is the best way to refresh a page with AngularJS?

I have a link that, when clicked by the user, triggers a page reload. I accomplished this using the method below...... HTML <div data-ng-hide="backHide" class="offset6 pull-right"> <a href="" data-ng-click="backLinkClick()"><< Back ...

Discovering the screen dimensions or viewport using Javascript in Bootstrap 4

Is there a method to identify the view port/screen size being utilized by Bootstrap 4 using Javascript? I've been struggling to find a dependable way to determine the current view port after a user resizes the browser. I attempted to use the Bootstra ...

Struggling with converting 11-line toy neural network code into JavaScript

I'm preparing to deliver a brief presentation on neural networks this coming Tuesday to my fellow web developer students. My plan was to convert this code (found under Part 1, a tiny toy neural network: 2 layer network) into JavaScript so that it woul ...

Ways to separate a string based on changing values in Javascript

Given this unmodifiable string: "AAACCDEEB" I am looking to split it into an array whenever the value changes. In this scenario, I would end up with 5 arrays like so: [['A','A','A'], ['C','C'], [ ...

What is the best method for selecting or isolating the code for a single animation created using JavaScript?

Currently, I am attempting to extract the CSS and JS code of an image reveal animation that is part of a static HTML page: https://i.stack.imgur.com/bnmaO.gif The challenge lies in the fact that the desired effect is one among many showcased image animat ...

Implementing a Comment Box and Posting Form in Django Rest Framework

After completing a django rest framework tutorial, I was able to create my own site (http://192.241.153.25:8000). The login functionality and post integration are working fine, but I am stuck on how to add a form for posting and a comment box for individu ...

Looking to streamline a JavaScript function, while also incorporating jQuery techniques

I've got this lengthy function for uploading photos using hidden iFrames, and while it does the job well, it's quite messy. I'm looking to simplify it into a cleaner function with fewer lines of code for easier maintenance. function simplif ...

AngularJS service does not halt for the success function to complete

I am facing an issue with my service where the success function in the controller is not being executed immediately. The compiler is parsing the lines after the service call, which is not the desired behavior. In debug mode, I noticed that there is a del ...

Combining Socket.io with AJAX for real-time web applications

I am currently working on a web application that relies on Socket.io for delivering notifications to users. I'm wondering if it would be more beneficial to utilize Socket.io exclusively for all client-server communication, or if mixing in traditional ...

Efficiently Extracting Data from an Array of Objects Using JQuery

What is the best method to extract information from the array below? How can I retrieve data categorized by specific categories? Is there a way to access only the key values? How do I retrieve just the data values? var main = [{ "key": "all", "dat ...

The functionality of wp_script_is in WordPress seems to be malfunctioning when it comes to

I need to load a certain file only after a specific script has finished loading. Wordpress offers a useful method called 'wp_script_is' for detecting if a script has loaded or not. When I use the jquery handle with "done", it functions as expecte ...

What is the best way to link my "dynamic sub-component" with AngularJS?

While I have a solid grasp on the fundamentals of AngularJS and can create basic CRUD applications, when it comes to applying this knowledge to real-world scenarios, I find myself struggling with how to integrate the concepts effectively. One specific cha ...

Is it possible to use an if statement within the .map()

I am seeking guidance on how to incorporate an if statement inside .map() Please refer to the code snippet below for context. Currently, the delete button is disabled when the image is not uploaded by the current user, but my objective is to complet ...

Function for jQuery Slide Toggle is not being invoked

I'm struggling with jQuery Slide Toggle. $(document).ready(function() { $("a").click(function() { $(this).parent().parent().children("p").Toggle(400); return false; }); }); I have multiple posts displayed on the page fetched from ...

Automatic capitalization feature for input fields implemented with AngularJS

I have integrated a directive into my input field to validate a license key against a server-side API. While this functionality works well, I also want the license key to automatically add hyphens and appear in capital letters. For example, when a user in ...

AngularJS - Leveraging the power of two-way data binding with JSON strings

I am a beginner with AngularJS and I'm currently working on retrieving, parsing, and displaying data from a SOAP web service. So far, I have been able to successfully make calls to a public weather service, capture and display the XML data that is ret ...

Navigate to the top of the Vue scrollable list using v-auto-complete

I need to implement a feature that automatically scrolls to the top of a scrollable list every time the search input is changed. Currently, I have a list where multiple items can be selected, and I want it to reset to the top whenever a new search query is ...

Using multiple ng-class directives on a directive

Check out this relevant plunker. In the file script.js, I've created a directive called super. This directive includes a template: template: '<div class="super" ng-class="{dirClass: foo}"></div>' The template contains an ng-cl ...

Identify when a click occurs outside specific elements

I've been searching for solutions to address this issue, but so far nothing has worked. Here is the JavaScript code I am using: var specifiedElement = document.getElementById('a'); document.addEventListener('click', function(eve ...