Nested arrays in Angular are not appearing in the array display

I've been attempting to display a nested array using AngularJS, but I'm encountering some difficulties.

Controller:

$scope.selected_category = [];
$scope.categories = [];

$scope.getCategories = function() {
    ItemService.getCategories().success(function(categories) {
        $scope.categories = categories;
    });
}

$scope.getCategories();

The form setting $scope.selected_category

<select class="form-control" ng-model="selected_category">
    <option ng-repeat="category in categories" value="{{ category }}">{{ category.name }}</option>
</select>

When I print out {{ selected_category }}, it displays the expected array with a subcategory array inside:

{
  "id": "1",
  "name": "clothing",
  "subcategories": [
    {
      "id": "1",
      "name": "jackets",
      "item_category_id": "1"
    },
    {
      "id": "2",
      "name": "jeans",
      "item_category_id": "1"
    },
    {
      "id": "3",
      "name": "sweaters",
      "item_category_id": "1"
    }
  ]
}

However, when I attempt to access selected_category.subcategories, nothing is returned. What could be causing this issue?

Here's a plunker demonstrating the problem: http://plnkr.co/edit/AtqpXogmItdSupEZGt7R?p=preview

Answer №1

Implement the ng-options directive:

<select class="form-control" ng-model="selected_category" 
ng-options="category as category.name for category in categories">
</select>

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

Retrieving the inner content of several paragraph elements, all consolidated into a single string

I'm trying to extract the inner HTML of multiple paragraph elements from a string and I need some help. Here's an example input: let HTML = "<p class="Paragraph" >Hello, World 1!</p><p class="Paragraph" >Hell ...

Looking to activate a button upon clicking a button on a different page using php and javascript

Is it possible for the first user logged in on one page to have a button, and when clicked, enable a disabled button on another page where the second user is logged in? This functionality needs to be implemented using PHP/JavaScript. Thank you in advance ...

Ways to Randomly Flip Divs

I have developed an application where all divs flip vertically on hover. I am looking for a way to make the flipping random without requiring a hover. Any ideas on how to achieve this? .vertical.flip-container { position: relative; float: left; ma ...

javascript guide on converting an array into the correct JSON format

I'm working with an array that has the following structure: var arr = [ [{a:1}], [{b:1}], [{c:1}], [{d:1}], [{e:1}] ] My goal is to format it into the proper format as shown below: [{a:1},{b:1},{ ...

Searching for the second highest element in an array in a single iteration

How can I efficiently find the second maximum element in an unsorted array within one iteration? For example, if the array is 3 9 8 2 0 -4 87 45 3 2 1 0, the answer should be 45. While it's easy to find the maximum element in one iteration, achieving ...

IE encounters an absolute z-index problem when expanding concealed content

I'm having trouble with the positioning of a block (div). I am new to CSS, so any help would be greatly appreciated. Here is the code snippet: http://jsfiddle.net/9rEmt/ (please check it out) for viewing online in IE. When I use absolute for positio ...

How can JavaScript onClick function receive both the name and value?

My current challenge involves a function designed to disable a group of checkboxes if they are not checked. Originally, this function was set to work onClick(), with one argument being passed from the checkbox element. Now, I need this function to be trigg ...

Tips for incorporating an HTML file using ng-include in double curly brace syntax {{ }}

Here is the code snippet I am working with: <div ng-repeat="pTabs in child.productTabs" ng-click="toggleProductTab($index)" ng-if="productTabIsActive(pTabs, $index)"> <div ng-repeat="specs in pTab.additionalSpecs"> <p>{{spec ...

The download attribute does not seem to function properly when used within the <a></a> tag in ReactJS

I've been attempting to download a PDF using the "download" attribute within an <a> tag: <a className="dl_link" href={'./myfiles/abc.pdf'} target="_blank" download/> However, instead of downloading, it only opens in the next ...

What are the steps to resolving an Unhandled promise rejection error in a Node.js application?

I encountered an error message that I need help resolving: I am faced with an unhandled promise rejection. This issue likely occurred due to throwing inside an async function without a catch block, or rejecting a promise without handling it using .catch( ...

Are there any drawbacks to utilizing data attributes for modifying content through JavaScript?

On my journey of developing a basic web app, I am faced with the challenge of updating data displayed on the screen based on certain events or conditions. The data is spread across various locations and embedded in completely different markup structures. I ...

Using jQuery to create a Select All Checkbox that toggles a specific class

I have come across similar examples in the past, but I have been unable to make it work as intended. The examples I found only use standard checkboxes. I attempted to customize the checkbox using a sprite sheet by applying a class, but I am struggling to a ...

Cache for AngularJS $http.jsonp requests

I'm struggling with caching a JSONP request and have tested out different approaches without success. I attempted $http.jsonp(url, { cache: true }) and $http({ method: 'JSONP', url: url, cache: true }), but neither worked as expected. As a ...

Printing multiple console logs using Socket.io

For one of my projects, I am using Socket.io and encountering an issue with multiple console logs for the same data update. Each time new data is received, it generates 7 to 9 repeated logs in the console. This behavior is unexpected and causing some conf ...

Using JQuery to Retrieve JSON Data from an HTTPS Endpoint

I am attempting to retrieve a JSON file from an https secured website without using server-side languages. The client specifically requested that the process be entirely implemented in JavaScript. After some research, I discovered that I need to utilize J ...

Locate the specific version of a JavaScript library within compiled JS files

The dist folder houses the results of running npm install. If I don't have access to the original package.json file used during the compilation of the distributable, how can I determine the version of a particular library that was utilized in the ins ...

How to implement Vue.js template into a different HTML document

I have successfully designed a template using vue.js. Now, I am seeking to utilize this template in another HTML file by referencing its template Id. For example, <div id="widgetId"></div> The template Id is labeled as widgetId. In this scen ...

How to identify NaN in JavaScript code?

I am attempting to use JavaScript to determine the number of days in a selected month of a selected year. However, I keep receiving a NaN value as a result. How can I resolve this issue? Thank you. <script> function myFunction() { var month ...

Access data from an array within an object using DataTables

Here's a simple question: How do I access an array inside an object using datatables? Object I am trying to access the array "data" : { "success": true, "data": [ { "id": "4", "tienda_id": "5", "tienda_nombre": "sad", ...

"Modifying the content:url() with JavaScript: A step-by-step guide

Hello, I am currently working on creating a traffic light using an array of images. My goal is to change the image path specified in the CSS when a button is clicked, to change the appearance of the traffic light. In this case, the 'content.url()&apos ...