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

Wait for NodeJS to finish executing the mySQL query

I am attempting to send an object from the controller to the view. To keep my queries separate from the controller, I am loading a JS object (model). My model structure is as follows: function MyDatabase(req) { this._request = req; this._connection = ...

"Revamp your site with Vue and API for dynamic background

I'm faced with an issue where I am attempting to modify the background of a joke fetched from an API, but for some reason, the background is not changing and I can't seem to identify why. The main problem lies in my inability to change the proper ...

Using relative paths to showcase images in Node.js

I am currently working on my first Node.js MVC app where I am using native Node and not utilizing Express. One issue I am facing is the difficulty in displaying images from my HTML files through their relative paths. Instead of sharing my server.js and ro ...

Handling an HTML Form without the Submit Button using VeeValidate

I've implemented a form handler using its composable feature in my <script setup>: const { submitForm, resetForm, handleSubmit, meta } = useForm() function save() { // Want to submit the form here submitForm() // Not working showSaveSnac ...

Text randomly appears on the html page

I've been dedicating a significant amount of time to finding a solution, but haven't had any luck. I'm aiming to create a visual effect where 10 words with varying font sizes slide in from different directions on a canvas within my document ...

The iframe remains unresponsive and fails to resize as needed

I am facing an issue while trying to embed an iframe into one of my websites. It seems that the responsiveness is lost and it does not scale properly on mobile devices. You can see the issue here. Interestingly, when I place the same iframe on a plain HTM ...

Using JavaScript to disable and re-enable an ASP.NET Timer control

I currently have a webpage built with ASP.Net that includes an ASP:Timer control <asp:Timer ID="TimerRefresh" runat="server" Interval="5000" Enabled="true" OnTick="TimerRefresh_Tick"> </asp:Timer> It is connected to an asp:UpdatePanel on the ...

Ways to retrieve the React Router match object in mapStateToProps

Is there a way to access the react-router match object for its params from mapStateToProps or any other selector? I'd like to use these params to generate a value that will be passed down as props to a presentational component within the selector. The ...

What is the method for showcasing an array using AngularJs in my specific scenario?

I have an array in JavaScript structured like this: var data = [ 2005 = [ Jan = [0,1,2,3,5...], Feb = [0,1,2,3,5...], ...more ], 2006 = [ Jan = [0,1,2,3,5...], Feb = [0,1,2,3,5...], ...more ], 200 ...

Tips for sending parameters to XSLT using a Javascript function

Despite my efforts to find a solution in various online posts, I haven't been able to resolve the issue. The challenge lies in my HTML file that includes a JavaScript function for parsing XML and rendering XSLT. I have multiple 'records' in ...

Does the [data-empty] constitute a component of the browser environment?

While exploring a node.js project that employs socket.io, I came across a piece of code that verifies whether the length of $('[data-empty]') is larger than 0. There was no class or ID associated with it, but it seemed like data-empty was some ki ...

Changing the content of the initial post in a loop on WordPress

<?php $freeadvice=new WP_Query('category_name=freeadvice&showposts=10'); while($freeadvice->have_posts() ): $freeadvice->the_post(); ?> <li class="event"> <input type="radio" name="tl-group" /> ...

Fixing blurry text on canvas caused by Arbor.js mouse event issues

Currently, I am utilizing arborjs in my project. The text within the canvas is created using fillText in html5. While everything functions correctly on a Retina display MacBook, the text appears blurry. To address this, I applied the following solution: v ...

Trouble with HTML Contact Page Email Delivery

Hello there, I recently set up a contact page for my html website but unfortunately, it's not sending the messages to my email as expected! You can see what I mean in this screenshot -> https://i.stack.imgur.com/2xPXw.png I'm a bit puzzled b ...

Utilize D3's pie chart to showcase the data in JSON's individual collections

I am currently working on creating a pie chart that displays the distribution of collections based on their names. I have come across an issue where d3.layout.pie().value() only evaluates specified array values. Is there a solution available to extract the ...

Informing the user of the navigation availability through a back button

When a user selects the back button, I display a confirmation dialog. Depending on their choice in the dialog, navigation may either be prevented or allowed to continue. Below is the code snippet for this functionality: $rootScope.$on('$stateChangeS ...

Accelerate blinking timer by utilizing CSS3 animations

I'm currently developing a quiz application that includes a timer counting down from 10 seconds to 0 seconds. As the timer reaches closer to 0 seconds, I am looking to implement a feature where my text blinks faster and faster. Additionally, I would l ...

Steps to properly create your own angular service with XHR

Exploring the world of AngularJS, I embarked on a mission to create a file upload feature along with other form data. While there are numerous scripts and plugins available, I decided to utilize my own custom service using $xhr. Despite successfully sendin ...

Instruct Smarty to display the block exactly as it is

Struggling to inline some JavaScript code into the Smarty template files due to the {ldelim} {rdelim} markers. Is there a way to instruct Smarty to disregard the markup and just output it as is? Any similar approach like CDATA blocks in XML? Just demonstr ...

Utilizing carouFredsel for thumbnails and compatibility with IE7

I've successfully integrated carouFredSel (using the basic example with next/prev and pagination) on Chrome, Firefox, IE9, and IE8. However, when I tested it on IE7, the plugin didn't seem to work correctly. The list just displayed vertically wit ...