One controller displays ng-repeats while the other does not

I have 2 controllers loading in different locations within my view. One controller works perfectly, but the other one does not show ng-repeats or appear in ng-inspector. I have confirmed that the http data is visible in the inspector.

Both controllers are using the WordPress REST-API.

Below is the main code:

var homeApp = angular.module('homeCharacters', ['ngSanitize']);

homeApp.controller('mainMenu', function($scope, $http) {
  $http.get("http://localhost:3000/wp-json/menus/2").then(function(response) {
    $scope.menuData.data = response.data;
  });
});

homeApp.controller('characters', function($scope, $http) {
  $scope.myData = {
    tab: 0
  }; //set default tab
  $http.get("http://localhost:3000/wp-json/posts?type=character").then(function(response) {
    $scope.myData.data = response.data;
  });
});
homeApp.filter('stripTags', function() {
  return function(text) {
    return text ? String(text).replace(/<[^>]+>/gm, '') : '';
  };
});
mainMenu Controller (not working):

<nav ng-controller="mainMenu as menuData">
  <ul>
    <li ng-repeat="x in menuData.data"><a href="javascript:void(0)">{{ x.items.title }}</a>
    </li>
  </ul>
</nav>
<!--end header nav-->


characters controller (working):

<section class="characters" ng-controller="characters as myData">
  <div class="char_copy">
    <h3>Meet the Characters</h3>
    <div class="char_inject" ng-repeat="item in myData.data" ng-show="myData.tab === item.menu_order">
      <div class="copy_wrap">
        <h3>{{ item.acf.team }}:</h3>
        <h2>{{ item.acf.characters_name }} <span>[{{item.acf.real_name}}]</span></h2>
        <p class="hero_type">{{ item.acf.hero_type }}</p>
        <div class="description" ng-repeat="field in item.acf.character_description">
          <p>{{field.description_paragraph}}</p>
        </div>
        <a class="learn_more" href="{{ item.acf.character_page_link }}">Learn More <img src="./app/themes/big-blue/dist/images/big-white-arrow.png" /></a>
      </div>
      <div class="image_wrap">
        <img src="{{ item.acf.homepage_full_image.url }}" />
      </div>
    </div>
  </div>
  <div class="char_tabs">
    <nav>
      <ul ng-init="ch.tab = 0">
        <li class="tab" ng-repeat="item in myData.data" ng-class="{'active' : item.menu_order == myData.tab}">
          <a href ng-click="myData.tab = item.menu_order">
            <img src="{{ item.featured_image.source }}" />
            <div class="tab_title_wrap">
              <div class="h3_background">
                <h3>{{ item.acf.characters_name }}</h3>
              </div>
              <p>{{ item.acf.team }}</p>
            </div>
          </a>
        </li>
      </ul>
    </nav>
    <a class="more_characters" href="#">More Characters <img src="./app/themes/big-blue/dist/images/big-white-arrow.png" /></a>
  </div>
</section>

If needed, here is the JSON for the non-working controller:

{
  ID: 2,
  name: "main",
  slug: "main",
  description: "",
  count: 6,
  items: [{
    ID: 43,
    order: 1,
    parent: 0,
    title: "The Resistants",
    url: "http://bigbluecomics.dev/the-resistants/",
    attr: "",
    target: "",
    classes: "",
    xfn: "",
    description: "",
...
}

Thank you for all the assistance, as I am still learning Angular!

Answer №1

Your li element that contains the repeater is not bound correctly.

It's advisable to avoid using ng-init and instead, move that logic into your controller for better organization.


<nav ng-controller="mainMenu as menuData">
  <ul>
    <li ng-repeat="x in myMenu.data"><a href="javascript:void(0)">{{ x.items.title }}</a>
    </li>
  </ul>
</nav>

The correct version should be:


<nav ng-controller="mainMenu as menuData">
  <ul>
    <li ng-repeat="x in menuData.data"><a href="javascript:void(0)">{{ x.items.title }}</a>
    </li>
  </ul>
</nav>

Answer №2

I was able to figure it out with the help of @Pytth's suggestion

homeApp.controller('mainMenu', function($scope, $http) {
  $http.get("http://localhost:3000/wp-json/menus/2").then(function(response) {
    $scope.menuData.data = response.data.items;
  });
});
<nav ng-controller="mainMenu as menuData">
  <ul>
    <li ng-repeat="x in menuData.data"><a href="javascript:void(0)">{{ x.title }}</a>
    </li>
  </ul>
</nav>
<!--end header nav-->

Thank you for your help! It's always nice to have another perspective!

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

Utilize a function within an AngularJS directive

I'm struggling to understand how to pass a function (delegate) to a directive in AngularJS and utilize it within the link-function. Any guidance or suggestions on the correct approach would be immensely appreciated. Below is the controller code: myA ...

Include a return or delete from the default IO statement

I am utilizing an intersection observer that alters the header's font-color and background-color based on the content intersecting with it. This change is determined by the presence of data-color and data-background attributes declared on the div/sect ...

Jquery function for determining height across multiple browsers

I am currently facing an issue with setting the height of table cells in my project. While everything works smoothly on most browsers, Firefox seems to add borders to the overall height which is causing inconsistency across different browsers. If anyone k ...

The functionality of ClickMarker is malfunctioning in the XY Amcharts

Recently, I encountered a situation where I had to incorporate custom legends in XY Amcharts. After managing to implement them successfully, I came across an issue. Despite adding event listeners to the legends, the associated function failed to trigger. ...

Unable to retrieve push token for the device. Please verify the validity of your FCM configuration

Looking for a solution to the issue of obtaining a push token Error: Unable to retrieve push token for device. Ensure that your FCM configuration is correct I have integrated Expo permissions and notifications in my React Native app, but nothing seems to ...

Is there a way to export a specific portion of a destructuring assignment?

const { a, ...rest } = { a: 1, b: 2, c: 3 }; If I want to export only the rest object in TypeScript, how can I achieve that? ...

Receiving a JavaScript function's output with Python Selenium

Currently, I am in the process of scraping data from a specific webpage. The focus is on extracting the return string value from a Javascript function snippet. The target value to be extracted is indicated as "2227885" Attempting to achieve this ...

Is Webpack bundling the node_modules of node_modules into the bundle?

Recently, I noticed that webpack is bundling dependencies from the top-level node_modules directory into my actual bundle. For instance, one of my dependencies, example-dep, relies on lodash and has a nested node_modules directory at node_modules/example-d ...

Unlimited Caching: A Guide to Storing HTTP Responses permanently

Is there a way to send an HTTP response that will be cached by any client indefinitely, so that the browser can retrieve it from the local file system without making any new HTTP requests when needed? Just imagine using this for versioned client code in a ...

Troubleshooting issues with calculator operators in JavaScript for beginners

As a beginner in JavaScript, I've taken on the challenge of building a calculator to enhance my skills. However, I'm having trouble getting the operator buttons (specifically the plus and equal buttons) to function properly. Can someone please as ...

Sending data to the template

Currently, I am working with NodeJS/Expressjs and have implemented the following route: router.post({ var value = req.body.value; //I NEED TO DO SOMETHING LIKE var file = '../test/test.js'; file.render(value); }); The content of / ...

Real-time Updating of ChartJS Charts in Rails Using AJAX

I am currently working with Rails 5 and the latest version of ChartJS library (). My goal is to retrieve the most recent 20 items from the SensorReading model and update the Chart using setInterval and AJAX. I have successfully created the AJAX call and ...

Personalized Jasmine matchers for a Protractor/WebDriverJS component

Greetings fellow developers, I'm looking to create a custom matcher for Protractor/WebDriverJS element. Can anyone assist in improving my current code? Here is what I aim to include in the specs files: var button = element(by.tagName('button&a ...

Guide on storing a promise response in an external object in VUE

I am currently working on integrating the higchart network graph in vue. Everything seems to be functioning properly with static data. However, when I attempt to retrieve data using axios, it fails to work. export default { data() { return { ne ...

What should be triggered when clicking on the cancel button in Bootstrap's modal: `close()` or `dismiss()`?

Bootstrap's modal offers two methods for hiding the dialog: close(result) (Type: function) - Used to close a modal by providing a result. dismiss(reason) (Type: function) - Used to dismiss a modal and provide a reason. Should I use close when the u ...

AngularJS: Add a unique CSS class to the initial item within an ng-repeat directive

I am searching for the initial index that meets my ng-if condition, which is not always equal to $index === 0, and cannot be resolved using $first. Below is a snippet of my code: <div class="ticket-event" ng-if="!item.hidden" ng-repeat="item in ctrl.e ...

Assess transcluded content prior to template compilation in an AngularJS directive

I am currently developing a custom "collapseText" directive using AngularJS. The purpose of this directive is to display a specified maximum number of characters and provide a "Read More" option to expand the text if it exceeds the limit. My goal is for t ...

What steps can I take to eliminate the '#' in my URLs when utilizing ui-router?

I have tried using $locationProvider and base href tag in the head of my index.html file, but I kept encountering a 404 error when trying to render my views. I believe the issue lies within the app.use method in my Express's app.js file. Here is the ...

Encountering an "undefined" error when implementing the useReducer hook in React

I'm encountering an error when using the UseReducer hook in React. Even though I have destructured the state object, I still receive this error: const [{previousOperand,currentOperand,operation},dispatch] = useReducer(reducer,{}); return ( ...

The functionality of toLowerCase localeCompare is restricted in NuxtJs VueJs Framework

Encountered a peculiar issue in NuxtJs (VueJs Framework). I previously had code that successfully displayed my stores in alphabetical order with a search filter. When I tried to replicate the same functionality for categories, everything seemed to be work ...