Angular ng-repeat not populating the list properly, causing a collapse not to display

Currently, I am working on developing an app using Angular.js and Bootstrap UI, but I have run into a problem with a collapse navigation feature.

The issue I am facing is that I have an ng-repeat that should be functioning properly. However, when I click on the nav-bar, it appears to collapse but does not display any elements inside. Below is my HTML markup:

<a href="#" ng-controller="CollapseCtrl">
    <ul class="list-unstyled">
        <li class="nav-header"> 
            <a href="#" ng-click="isCollapsed = !isCollapsed" ><h5>Settings 
                <i class="glyphicon glyphicon-chevron-right"></i></h5>
            </a>
            <ul class="list-unstyled collapse in" collapse="isCollapsed">
                <li ng-repeat="element in first"><a href="#"><i class='{{element.icon}}'></i>{{element.title}}</a></li>
            </ul>
        </li>
    ......Some other nav-header with other lists
    </ul>
</a>

Here is my script:

var myAppModule = angular.module('myApp', ['ui.bootstrap']);
myAppModule.controller('CollapseCtrl', function($scope) {

    $scope.first = [
        {title : 'Home', icon : 'glyphicon glyphicon-home' },
        {title : 'Messages', icon : 'glyphicon glyphicon-envelope' },
        {title : 'Options', icon : 'glyphicon glyphicon-cog' },
        {title : 'Shoutbox', icon : 'glyphicon glyphicon-comment' },
        {title : 'Staff List', icon : 'glyphicon glyphicon-user' },
        {title : 'Transactions', icon : 'glyphicon glyphicon-flag' },
        {title : 'Rules', icon : 'glyphicon glyphicon-exclamation-sign' },
        {title : 'Logout', icon : 'glyphicon glyphicon-off' }
    ];

    $scope.isCollapsed = false;

});

I attempted to troubleshoot using Fiddle, and it seems that the issue is not related to the ng-repeat implementation.

I'm struggling to identify where I went wrong, so any assistance would be greatly appreciated. It's likely just a simple mistake somewhere...

Thank you!

Answer №1

There appears to be an issue with having the controller attached to an anchor element. By changing

<a href="#" ng-controller="CollapseCtrl">
to
<div style="cursor: pointer;" ng-controller="CollapseCtrl">
, the problem is resolved. Another solution would be placing the div directly around the anchor if you require the href="#" attribute for something other than just altering the cursor appearance.

To view the plunkr showcasing this fix, visit: http://plnkr.co/edit/6dLaOacDhHnc6038Y5MN?p=preview

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

Adding an exception for ClickAwayListener in React-MUI

Hey there! I'm currently exploring MUI and trying to incorporate the ClickAwayListener API into my project, but I'm facing some difficulties. You can take a look at my project on codesandbox here: https://codesandbox.io/s/react-leaflet-icon-ma ...

Scrollmagic - Creating dynamic effects by animating body and div backgrounds using multiple tweens

I'm currently developing a website that utilizes scrollmagic to smoothly transition the color of the active div (each set to size of the screen) from black back to white as you scroll down. The issue I am facing is that the body background color does ...

Seeking guidance on recompiling a directive within Angular?

I have implemented a directive to display data in a tree-like structure. This directive allows for filtering of the data. However, when I clear the filter, the directive does not automatically update to reflect the changes in the data. It only updates when ...

AngularJS application, when making a CORS GET request, is not transmitting HTTP headers

My issue lies in the fact that when making HTTP GET requests in AngularJS, the headers are not being sent. Strangely enough, for HTTP POST requests, the headers are properly set. These requests are made over CORS. Despite scouring numerous posts on StackO ...

Placing the date of each blog post at the end of the excerpt

Currently, there is a grid section on the homepage of our site showcasing the two most recent blog posts. I am looking for a Javascript solution to relocate the date to the bottom of each block, outside of the border. For reference, you can visit the follo ...

I'm feeling a bit lost on where to go next with this JavaScript

I've been working on a project in Python where I need to retrieve a file, store it in an array, and display a random string from the array on HTML. However, I have no experience with JavaScript and am unsure how to proceed. I suspect there may be an i ...

Using React to dynamically assign a backgroundImage based on a JSON response

I am having an issue with retrieving data from my Wordpress API and displaying it in my react app. Specifically, I am struggling to set the post's featured image as a background-image for an element. Here is an example of the JSON response: { "id" ...

What is the best way to display data in a React application depending on a boolean value?

Being new to React and JavaScript, I am currently struggling with boolean logic. I have a function called Profile which includes two constant methods that each return different data. function Profile(props) { const returnNormalProfile() const return ...

Implementing useState to toggle the checked value of a checkbox in React

When passing a list of checkbox fields with attributes to my component, I want to update the checked attribute based on user selection. However, all I have managed to do so far is double the check value in an array. How can I modify this specific key with ...

What is the best way to add data from an array to a DOM element in the same order it was retrieved from Firebase?

Utilizing Google Firebase Firestore for data storage and the Open Movie Database (OMD) in combination with Axios to retrieve movie information. I am currently developing a website that allows users to add movies to collections. On the collections page, al ...

Adding a JavaScript script tag within a React app's ComponentDidMount - a guide

I am currently in the process of implementing Google Optimize on my website. I need to include the following script tag within my page: <script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date; h.end=i=function(){s.classN ...

Sending a JavaScript object as a prop in a React component

Currently, I am enrolled in a React course that requires us to pass a single JavaScript object as props to a React application. Here's the code snippet I have been working on: import React from 'react'; import ReactDOM from 'react-dom& ...

Struggling to locate a route for the React styled components image

I'm having trouble locating the correct path for the image in my React styled components. I believe the path is correct, but could the issue be related to styled-components? Check it out here import styled from "styled-components"; export defaul ...

What is the method for specifying a JSON object within a DOM element's `data` attribute?

In an attempt to store JSON data within a dataset attribute, I encountered the following HTML structure: The appearance of the HTML is as follows <div data-switch="true" data-json="{'key1': 'value 1'}, {'key2': 'valu ...

Change the default values for grid column configurations in Ext JS globally

The Ext.grid.column.Column class contains the following configurations: draggable (Default: true) sortable (Default: true) menuDisabled (Default: false) Is there a way to globally change the default values of these configurations for all grid columns i ...

automatically collapse a submenu once a different menu option is selected

After doing some research and trying out various solutions, I still couldn't get it to work. I made adjustments to my dropdown menu and click function so that each submenu opens and closes when its parent is clicked. However, I'm now looking to f ...

Enliven the character limit reaction by incorporating a thrilling shake animation when it reaches

I have implemented a feature in my component where if a user reaches the character limit, the component should shake. However, despite my efforts, the shaking effect is not working in the current code. const useStyles = makeStyles(() => ({ shake ...

What is the best way to retrieve a FireStore document ID from an object?

I'm in the process of trying to reference an auto-generated firestore document ID in order to create a subcollection within it. The issue I'm facing is that although I can locate the document ID, I'm struggling to save it to a variable in a ...

Communication between nodes using serial ports in Node.js fails to receive a response from the connected Arduino board

I've been attempting to establish communication between a computer and an Arduino board using node.js. Despite having a simple program, it just doesn't seem to work as expected. The Arduino program (which seems to be working fine) is as follows: ...

Having trouble retrieving object values from the request body during a POST request in [Node.js, MySQL]

I am currently developing a management system and working on creating POST requests for the API to add a new city to the database. However, I am facing an issue where instead of receiving the correct values from the request's body, I am getting "undef ...