NG - a loop to loop through a complicated array such as this

I am working with an array that looks like this:

$scope.disArray = [0, -5, -10, -15, [-11, -12,-13, -14], [-12, -13, -14]];

My goal is to display the elements of the array in a table where direct values are shown as input type text and arrays as select dropdowns.

The Expected Output should look like this:

<tr>
    <td>0</td>
    <td>-5</td>
    <td>-10</td>
    <td>-15</td>
    <td>
      <select>
         <option>-11</option>
         <option>-12</option>
         <option>-13</option>
         <option>-14</option>
      </select>
    </td>
    <td>
      <select>
         <option>-11</option>
         <option>-12</option>
         <option>-13</option>
         <option>-14</option>
      </select>
    </td>

Any assistance in achieving this would be greatly appreciated. Thank you in advance.

Answer №1

Give this a try

var app=angular.module("app",[]);

app.controller("myCtrl",function($scope){
   $scope.isArray = angular.isArray;
   $scope.disArray = [0, -5, -10, -15, [-11, -12,-13, -14], [-12, -13, -14]];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
  <table>
    <tbody>
       <tr>
          <td ng-repeat="value in disArray">
             <span ng-if="!isArray(value)">{{value}}</span>
             <select ng-if="isArray(value)" ng-options="val for val in value" ng-model="selected"></select>
    
           </td>
         </tr>
      </tbody>
    </table> 
</div>

Answer №2

Here is one way to achieve it:

HTML :

<tr>
    <td ng-repeat="item in itemList">
        <span  ng-if="!isArray(item)" ng-bind="item"> </span>

        <select   ng-if="isArray(item)"  ng-model="selectedItem"  ng-options="option for option in item ">
        </select>    
    </td>
</tr>

JS :

$scope.itemList = [0, -5, -10, -15, [-11, -12,-13, -14], [-12, -13, -14]];

    $scope.isArray = function(element){
        return angular.isArray(element);
    }

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

When using Angular 2 Forms, the formControl.touched property will only be set to true when the text area is clicked, rather

Below is a form I am working with: <form *ngIf="showForm" [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)"> <textarea type="text" placeholder="Description" [formContro ...

Change the code from a for-loop to use the Array#map method

I have been working on a simple JavaScript function and attempting to convert the code from using a for-loop to Array#map. I'm sharing my code in the following fiddle as I am currently learning about array map: http://jsfiddle.net/newtdms2/. function ...

Tips for turning on a gaming controller before using it

Current Situation In my ionic side menu app, I have a main controller called 'main view'. Each tab in the app has its own controller, which is a child of the main controller. The issue I'm facing is that when I start the app, the first cont ...

AngularJS iOS keyboard not disappearing post iOS 10 update

After updating my device to iOS 10, I encountered an issue with hiding the iOS keypad when switching from one view to another. This functionality was working perfectly fine on iOS 9.3. Previously, I had programmed it to intercept certain elements and auto ...

Decoding a Json list with angularJS

I have a JSON dataset structured as follows: $scope.jsondata = [{filename:"/home/username/textfiles/0101907.txt"},{filename:"/home/username/textfiles/0124757.txt"},{filename:"/home/username/textfiles/0747332.txt"} ... ]; Here is my HTML code: <li ng ...

SheetJS excel-cell customization

I'm using this example to export a worksheet from https://github.com/SheetJS/js-xlsx/issues/817. How can I apply cell styling such as background color, font size, and adjusting the width of cells to fit the data perfectly? I have looked through the do ...

Finding a specific object within an array of objects by searching through a key value pair

In my collection, I have an array of objects structured as follows: [{ "businessunit": [{ "Area": [{ "Asset": [{ "Wells": { "Well": "Well 11" }, "name": "Field ...

Typescript versus ES5: A comparison of Node.js server-side applications written in different languages

Note: When I mention regular JavaScript, I am referring to the ES5 version of JS. As I lay down the groundwork for a new project, my chosen tech stack consists of Node.js for the back-end with Angular2 for the front-end/client-side, and Gulp as the build ...

Using Angular promises and the $http service

ng.module('app') .service('CardService', ['$http', CardService]) function CardService($http) { this.$http = $http; var self = this; $http.get('http://localhost:3000/db').success(function(data) { ...

Having issues with default sorting and searching not functioning in Datatables with Angularjs

Utilizing a directive to facilitate database building once ng-repeat has completed: app.directive('repeatDone', function() { return function(scope, element, attrs) { if (scope.$last) { scope.$eval(attrs.repeatDone); ...

Angular login/signup modal/dialog component for seamless user authentication

Currently, I am working on adding a login/signin dialog to my app similar to the one used by Medium. After doing extensive research online, I have decided to use the $modal from angular ui-bootstrap for this. Can anyone please recommend a tutorial that wil ...

Characteristics within the primary template element of a directive

I'm having an issue with the following directive code: .directive('myDirective', function () { restrict: 'E', replace: true, transclude: true, scope: { label: '@', ngModel: '=', ...

Is it possible to return a promise within the .then function in AngularJS?

I have a unique service called "usersService". I want to create a special method that interacts with another custom service I built. This other service has two handy methods: getUser() and getCurrentUser(). The getCurrentUser() method relies on the injecte ...

Firebase 3 authentication does not maintain persistent login sessions

I recently upgraded my AngularJS app to use Firebase 3.x and AngularFire 2.x for email and password authentication. I referred to the following documentation for migration: https://firebase.google.com/support/guides/firebase-web https://github.com/fi ...

The Angular JS Factory fails to send data back to the controller

When I call the method getPopularMovies in my factory using the controller, it returns undefined. I'm not sure what mistake I've made here. Please help me figure it out. My Factory angular.module('ngMovies').factory('moviesFactor ...

Incorporating HTML with ng-binds through transclusion

I have been developing an angular directive to enhance the functionality of ng-table by adding filtering and exporting features. The main goal is to make this directive reusable for multiple tables, which requires the <td> elements to be dynamic. To ...

Where can I locate a specific child element in this scenario?

Currently, I am exploring the possibilities of integrating AngularJS into my application and have encountered a question regarding the click event implementation. Within my HTML code: <div ng-click='clickMe()' ng-controller='testCtrl&ap ...

Developing an Angular Chart with AJAX

Utilizing the power of angular-chart.js, I have successfully generated a chart with the provided code snippet. However, my goal is to dynamically generate a chart using AJAX calls. Unfortunately, when I attempt to load the chart through AJAX, it fails to r ...

Is it possible to dynamically insert one module into another module?

Let's say I have 2 modules in two separate files: The first one is for everyone, let's call it myApp: var myApp = angular.module('myApp', ['dependency.one', 'dependency.one']); In another file named admin.js, I ha ...

Handling JSON Data in JavaScript

In the script below, I have a json object that is being processed: $http({ url: '/mpdValidation/mpdValidate', method: "POST", data: { 'message' : mpdData } ).then(function(response) { console.log(response.data ...