Connecting a string array to the navigation bar

Need Assistance with AngularJS: In my controller, I have a string array that looks like this:

var app = angular.module('myApp', []);
app.controller('mycontroller', function($scope) {
$scope.menuitems =['Home','About'];
  };
});

I want to display these items in the navigation pane on the left side. Here is the HTML code I am using:

<body class="container-fluid">
    <div class="col-md-2">
        <ul class="nav nav-pills nav-stacked" ng-repeat="x in menuitems track by $index">
            <li>{{x}}</li>
        </ul>
    </div>
</body>

Currently, the navigation pane only shows {{x}} instead of the actual text values from the array. Any suggestions or help would be greatly appreciated.

Answer №1

  1. It is not necessary to include }; in your javascript code.
  2. Make sure you have connected the controller to the div.

Sample Code:

Javascript Section:

var app = angular.module('myApp', []);
app.controller('mycontroller', function($scope) {
    $scope.menuitems = ['Home', 'About'];
    //}; // This line can be removed
});

HTML Section:

<div class="col-md-2" ng-controller="mycontroller">
<!--                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -->
    <ul class="nav nav-pills nav-stacked" ng-repeat="x in menuitems track by $index">
        <li>{{x}}</li>
    </ul>

Answer №2

Here is the code snippet you can use:

<body class="container-fluid">
    <div class="col-md-2">
        <ul class="nav nav-pills nav-stacked" >
            <li ng-repeat="x in menuitems">{{x}}</li>
        </ul>
    </div>
</body>

Answer №3

It is important to remember to include the mycontroller in your HTML code for it to function correctly.

<body class="container-fluid">
<div class="col-md-2" ng-controller='mycontroller'>
<ul class="nav nav-pills nav-stacked" ng-repeat = "x in menuitems track by $index">
<li>{{x}}</li>
</ul>
</div>
</body>

Answer №4

It appears there may be a typo in your code; consider placing the ng-repeat directive within an li tag to see if that resolves the issue. Give it a try!

Answer №5

Just a quick note: Make sure that all the necessary directives like ng-app and ng-controller are included in your code before proceeding further. If you're working with HTML5 using <!DOCTYPE html>, keep in mind that syntax issues might arise occasionally. I faced a similar issue where I missed closing the script tag properly, resulting in rendering {{x}} on the browser. Once I corrected this and closed the script tags correctly, everything worked smoothly for me. You could give it a shot and see if it solves your problem.

<!DOCTYPE html>
<html>
<head>
    <title> My Custom Web Page </title>
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
***** <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"/>
<script type="text/javascript" src="app.js"></script>
<script src="soCtrl.js"></script>
</head>
<body ng-app="soApp">
    <div class="col-md-2" ng-controller="soCtrl">
    <ul class="nav nav-pills nav-stacked" ng-repeat="x in menuitems track by $index">
        <li>{{x}}</li>
    </ul>
    </div>
</body>
</html>

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

Navigating through AngularJS routes triggers Ajax data retrieval upon changing routes

I'm struggling to grasp the Angular approach for the following scenario... I have a router set up, and when the route changes and the templateUrl is provided, I want to initiate an Ajax call to retrieve some JSON data. I don't want to wait for ...

Angular CORS problem when sending information to Google Forms

When submitting the data: Error Message: Unfortunately, an error occurred while trying to send the data. The XMLHttpRequest cannot load https://docs.google.com/forms/d/xxxxxxxxxxxxx/formResponse. The response to the preflight request did not pass the ac ...

What could be causing the erratic jumping behavior of the "newsletter sign-up" form within my modal dialog window?

On the homepage, there is a modal window that appears upon every pageload (it will be changed later), however, there seems to be an issue with the 'email sign up' form inside the window. The form seems to momentarily display at the top of the si ...

Is there a way to verify the identity of two fields using an external script such as "signup.js"?

My current project involves working with Electron, and it consists of three essential files. The first file is index.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="styles ...

Turn off transparency for the child element if the parent element has transparency

In my design setup, there is a container with an opacity set at 0.8. This allows the background image to subtly shine through the content area. The challenge arises when I place a client photo inside this container. The issue is that the photo's opac ...

Exploring the intricacies of handling exceptions in the service layer with Spring and Angular JS

Our team of developers is currently working on an application that utilizes jasper-reports 6.2.0, spring-mvc 3.2.14, java-ee-7, tomcat 8, and angularjs in the front-end. All our rest requests are made using ajax. The application is designed to receive JSO ...

Breaking down an array in Node.js

After web scraping, I retrieved an array structured like this: array: [ 'line1', 'line2', 'line3', 'linen'.. ] My task now is to insert this data into a MySQL table. The challenge is that every 10 lines of ...

How can we call a function in HTML from an external JavaScript file?

I am attempting to utilize a function that I have defined in my .js file within my HTML document. js/newsalerts.js function decodeHTML(html) { //https://stackoverflow.com/a/2989105/4650297 var tempDiv = document.createElement("DIV"); tempDiv. ...

Thumbnail Option in the WordPress Media Uploader

Is it possible to add support for selecting image size (thumbnails) in the Media Library popup created using the wp.media() function in WordPress? I am currently using WordPress 4.5.2 and my code sample is as follows: wp.media.frames.selectFile=wp.media( ...

Can this actually work? Overflow-y set to visible and overflow-x set to scroll/auto

We are facing a challenge within our team that we are struggling to resolve. Our Grid control has been customized by us. When the icon next to the column name is clicked, a pop-up div (referred to as divFilter) appears, allowing for filtering options to be ...

Flexibility on smaller screens using Flexbox

On larger screens, I have arranged 4 icons in a row, but on smaller screens (less than 768px), I need to display only 2 icons in a row. This is my current code : .welcome { display: flex; justify-content: space-around; } @media (max-width: 768px) ...

AngularJS enhances user experience by allowing textareas to expand upon click

I am just starting to learn about angular.js and I'm wondering how to add a text area when clicking on an image. ....... ..... ..... </thead> <tbody> <tr ng-repeat="stu in Studentlist"> <td>{{stu.rollno}}</td> <td> ...

Ways to ensure all tabs on the Nav bar are evenly distributed across the width of the screen

Hello! I am in the process of creating my first website, and it's going to be a D&D character randomizer. Currently, I'm working on the Nav-bar section and I have four bars that I want to fit across the screen equally, regardless of the scree ...

I sought out a way to incorporate a hyperlink within the Material Data Grid

Take a look at this sample data grid for reference: here. I added an image like this: see here. However, I couldn't create a clickable link. ...

Is it possible to make the li elements within my ul list display on multiple lines to be more responsive?

My credit card icons are contained within a ul. I have used the properties background-image and display:inline for the LIs, which display nicely on larger screens. Unfortunately, when viewed on mobile devices, the icons are cut off. My question is whether ...

How can I modify the CSS styles for a custom jQuery widget?

I have been working on creating a custom widget with a textarea that has multiple rows. I am now looking to change the background color of this widget on the Onfocus and OnfocusOut events. Can anyone guide me on how to achieve this? <!doctype html> ...

Uncertainty surrounding dynamic bootstrapping in @NgModules

After successfully installing rc7, my module and component are functioning as expected. Now, I am looking to utilize it on a webpage by only bootstrapping modules and components if the current page contains the necessary selector. Although I have declare ...

Encountering a TS2307 error while trying to import external modules into a TypeScript file

I recently added a new module using npm and now I'm trying to use it in my typescript file. npm install marker-animate-unobtrusive --save import SlidingMarker = require('marker-animate-unobtrusive'); Unfortunately, when I try to access th ...

Is there a way to retrieve the initial value from the second element within the JSON data?

Exploring JSON Data Collections In my JSON data, I have two collections: FailedCount and SucceededCount. { "FailedCount": [{ "FailedCount_DATE_CURRENT_CHECK": "2016-11-30 10:40:09.0", "FailedCount_DATE__CURRENT__CHECK": "10:40:09", "FailedCo ...

Is there a way to incorporate the use of quotation marks within the ng-bind directive in AngularJS?

Is there a way to insert phoneNo["phoneno"] into an HTML input using the ng-bind directive in Angular? While {{phoneNo["phoneno"]}} can display the data, it results in a syntax error when I try to put it like this: <input class="form-control" type="t ...