"Ng-repeat function seems to be malfunctioning, although I am able to view

There seems to be an issue with ng-repeat when dealing with an array of objects:

dummy.json

[
  {"name":"alpha", "data":[{"name":"Unit 1", "prereqs":[]}]},
  {"name":"beta", "data":[{"name":"Unit 1", "prereqs":[]}]}
]

While I am able to fetch the total number of objects({{factions.length}}) or individual items like {{factions[0].name}}, the ng-repeat does not generate multiple options in the select. I have come across similar queries but haven't found a solution yet.

The controller is included within the body and the scope variable is accessible, however, there seems to be something missing... Any help would be greatly appreciated.

tree.js

'use strict';

angular.module('tree',[])
  .controller('treeCtrl', function ($scope,$http) {
        $scope.factions = [
  {"name":"alpha", "data":[{"name":"Unit 1", "prereqs":[]}]},
  {"name":"beta", "data":[{"name":"Unit 1", "prereqs":[]}]}

];

    //$http.get('/dummy.json')
    //  .success(function(data){
    //    console.log(data);
    //    $scope.factions = data;
    //  })
  });
<!DOCTYPE html>
<html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="tree">
<head>
  <meta content="IE=edge" http-equiv="X-UA-Compatible">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>Trees</title>
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
  <!-- <link rel="stylesheet" href="app/styles/techtree.css">
  <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet"> -->
</head>
<body ontouchstart data-ng-controller="treeCtrl">
  
  <div class="section">
    <div class="container">
      <form>
        <div class="row">
          <div class="col-sm-4">
            <div class="form-group">
              <label for="factionSelect">Faction  {{factions.length}}</label>
              <select id="factionSelect" class="form-control">
                <option ng-repeat"faction in factions" value="{{$index}}">{{faction.name}}</option>
              </select>
            </div>
          </div>
        </div>
      </form>
    </div>
    {{factions[1].name}}
  </div> 

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
  <script src='//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'></script>
</body>
</html>

Answer №1

Make sure to include the = character between the ng-repeat attribute and its value.

'use strict';

angular.module('tree',[])
  .controller('treeCtrl', function ($scope,$http) {
        $scope.factions = [
  {"name":"alpha", "data":[{"name":"Unit 1", "prereqs":[]}]},
  {"name":"beta", "data":[{"name":"Unit 1", "prereqs":[]}]
];

    //$http.get('/dummy.json')
    //  .success(function(data){
    //    console.log(data);
    //    $scope.factions = data;
    //  })
  });
<!DOCTYPE html>
<html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="tree">
<head>
  <meta content="IE=edge" http-equiv="X-UA-Compatible">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>Trees</title>
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
  <!-- <link rel="stylesheet" href="app/styles/techtree.css">
  <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet"> -->
</head>
<body ontouchstart data-ng-controller="treeCtrl">
  
  <div class="section">
    <div class="container">
      <form>
        <div class="row">
          <div class="col-sm-4">
            <div class="form-group">
              <label for="factionSelect">Faction  {{factions.length}}</label>
              <select id="factionSelect" class="form-control">
                <option ng-repeat="faction in factions" value="{{$index}}">{{faction.name}}</option>
              </select>
            </div>
          </div>
        </div>
      </form>
    </div>
    {{factions[1].name}}
  </div> 

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
  <script src='//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'></script>
</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

Simply output the integer value

Recently, I've been working on a function that I'm trying to condense into a one-liner code for a challenge on Codewars. You can find the problem here. Here's the code snippet that I currently have: export class G964 { public static dig ...

Exploring the implementation of Javascript, HTML, PHP, and Ajax in programming

<div class="navigation"> <a class="prev" href="index.php?month=__prev_month__" onclick="$('#calendar').load('index.php?month=__prev_month__&_r=' + Math.random()); return false;"></a> <!-- <div class="title" & ...

Determining the size of an image prior to uploading it in a React

The solution for this issue can be found using vanilla JavaScript here To clarify, instead of using alert(), my goal is to store the dimensions in the state object. How can I adapt this code into a React component utilizing the OnChange() event handler? ...

Error message: Property is not found in the $rootScope object in AngularJS

Encountering an issue while attempting to assign a value to the rootscope in Typescript. class TestClass{ this.rootScope: ng.IRootScopeService; constructor($rootScope){ this.rootScope = $rootScope; } addValueToRoot=()=>{ ...

bootstrap used for creating horizontal radio buttons

I'm attempting to horizontally align radio buttons within a form without relying on the Bootstrap library. The following code achieves this: <form id="test_form"> <div class="control-group"> <label for="Q1">First question</ ...

How to remove the black border on a material-ui button

I am currently working with the material-ui datepicker and buttons. Whenever I click on the datepicker icon to select a date or any of the buttons, I notice a black border appearing. How can I remove this black border from the design? ...

Another problem with CORS again?

My rails application uses the host http://myhost.dev to search for music through the vk.com API. The API provides a search method called audio.search. Below is the code snippet from my search.js.erb file which is executed via a remote link to the search ac ...

Oops! Before proceeding, make sure to call TestBed.initTestEnvironment() first

Currently, I am experimenting with testing a service in Angular. Below is the code snippet I am working on: describe('AddressService', () => { let service: AddressService; let injector: TestBed; let httpTestingController: HttpTesting ...

Using Vuex's v-model to bind to a state field in an object

In my current Vuex state, I have defined a getter named configs, which looks like this: configs: { 1303401: { exampleValue: 'test' } } There is also an input where I bind the exampleValue from the Vuex store's state using v-mo ...

Discovering the source DOM element that initiated ng-change

I am currently working with angularJS and have multiple <select> elements on my webpage, each with its own ng-change function. Here is an example: <select id="hairColorComponent" ng-model="hairColor" ng-options="option.name for option in ...

PHP functions triggered by ajax fail to function properly when called repeatedly

I have encountered an issue with my javascript function that calls a php script upon clicking a button or link to retrieve data. Everything works well, except when I attempt to call data from an already outputted content via the same function. Let me share ...

Show text using AJAX when the form is submitted

I'm in the process of creating a basic form with a submit button (see below). My objective is to allow users to enter text into the input box, click submit, and have the page refresh while displaying the entered text in a div element. The user's ...

Designing a calendar with a grid template

I am attempting to design a calendar that resembles an actual calendar, but I am facing an issue where all created divs (representing days) are being saved in the first cell. I am not sure how to resolve this. Any help would be greatly appreciated. css . ...

The responsive menu refuses to appear

my code is located below Link to my CodePen project I'm specifically focusing on the mobile view of the website. Please resize your screen until you see the layout that I'm referring to. I suspect there might be an issue with my JavaScript cod ...

I am currently working on creating a drag select feature in React completely from scratch, but I'm facing some challenges with

Check out this code I created for drag selection: Here's the item generation code: const items = [ 1, 2, 3, ...94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, ].map((i) => ({ item: i, selected: i === 1 })); This is the actual code responsi ...

Attempting to persist a nested document in MongoDB using mongoose within a Nodejs environment

I attempted to save an address as a nested document in the following format When sending data from the client side, it was formatted like this: const address = JSON.stringify( { addressline1:form.addressline1.value, addressline2:form.addressline2.value, c ...

What is the best way to incorporate an input id value (based on iteration) into a while loop that points to a specific piece of html located outside of the loop

In my scenario, I need to dynamically add input fields to a form based on the selection made in a dropdown. The issue arises when these new input fields end up sharing the same ID, which is causing problems in the code. To resolve this, I am looking to app ...

Enable my textbox to interpret the html img tag

Is there a way to display emoji images instead of emoji symbols when inserting them into my textbox? For example, can I show the image representation of ':)' instead of just the symbol itself? ...

Analyzing the original data form versus the modified data

I'm dealing with a form in React that utilizes react-hook-form and the useFieldArray method for adding dynamic steps. The challenge I'm facing is how to compare the original data passed to the form with the edited data, in order to make correspon ...

Tips for resolving the error of encountering a forbidden and unexpected token in JSON at position 0 while using React hooks

const [forecastData, setForecastData] = useState({ forecast: []}); useEffect(() => { let ignore = false; const FETCHDATA = async () => { await fetch(forecast,{ headers : { ...