Cannot use Object.keys function in AngularJS

For my UI.Bootstrap accordion, I have set up the heading as follows:

<accordion-group ng=repeat="(cname, stations) in byClient">
    <accordion-heading>
        {{ cname }} <span class="pull-right"> {{ Object.keys(stations).length }} Stations</span>
    </accordion-heading>

However, when this is displayed, Object.keys(stations).length does not return any value. Strangely enough, if I place the same length call in my controller, it works and returns the expected count. Is there a reason why the method call isn't working in AngularJS?

The rest of the accordion that uses stations functions properly, indicating that the data is being populated correctly. Here is an example structure of the byClient data:

{
    "Client Name" : {
        "Station Name": [
            {...},
            {...}
        ]
    }
 }

Answer №1

Indeed, the reason for this is that Object belongs to the window/global scope and angular is unable to evaluate that expression within its own scope. When you use Object.keys in your binding, angular attempts to evaluate it within the $scope but does not find it there. One solution could be to store a reference to Object.keys in a utility function in the rootScope so it can be accessed throughout the application.

You can achieve this by:

angular.module('yourApp',[deps...]).run(function($rootScope){
  //Add a reference to the utility methods in rootscope.
  $rootScope.Utils = {
     keys : Object.keys
  }

  //If you want the utility method accessible in isolated Scopes 
  //you would directly add the method to the prototype of rootScope constructor as shown below:

  //$rootScope.constructor.prototype.getKeys = Object.keys;

});

Then you can use it like this:

<span class="pull-right"> {{ Utils.keys(stations).length }} Stations</span>

This will be available to any child scopes except for isolated scopes. For isolated scopes (e.g., Isolated scoped directives), you would need to add the reference of Object.keys on the scope itself, or expose a method on the scope that returns the length.

Alternatively, you could create a custom filter to return the key length and use it across your app:

app.filter('keylength', function(){
  return function(input){
    if(!angular.isObject(input)){
      throw Error("Keylength filter can only be used with objects!")
    }
    return Object.keys(input).length;
  }
});

And then use it like this:

{{ stations | keylength }}

Check out the Demo here

Answer №2

Employ this function to calculate the quantity of properties within an object:

$scope.calculateKeyLength = function (obj) {
    return Object.keys(obj).length;
}

Then utilize the following code snippet:

{{ calculateKeyLength(myObject) }}

Answer №3

I believe that utilizing filters is the epitome of AngularJS when it comes to managing structures within template code:

angular.module('app.filters').filter('objectKeysLength', [function() {
    return function(items) {
        return Object.keys(items).length;
    };
}]);

angular.module('app.filters').filter('objectKeys', [function() {
    return function(item) {
        if (!item) return null;
        var keys = Object.keys(item);
        keys.sort();
        return keys;
    };
}]);

Answer №4

If you're looking for a solution for angular 2 and above, there is now the keyvalue pipe available, allowing for iteration over objects.

Answer №5

After struggling with various solutions in AngularJS 1.6, I finally found success by utilizing $window to access Object.keys. Here's the code snippet that worked for me:

$window.Object.keys({ 'x': 3, 'y': 4 })

Answer №6

If you're looking for a solution that has proven effective, consider the following:

export class abcComponent{
    Display = Object.values;
}

When working in the HTML component file, you can implement something along these lines:

<div *ngFor="let item of Display(data)">.........</div>

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

Issue with click event not being triggered in Kendo Sortable and AngularJS combibation

Check out this plunk where I have a directive containing a KendoSortable. I'm facing an issue where the click event is not triggered when the user clicks on any of the sortable divs. Can you spot what's causing the problem in the code? HTML < ...

Is it possible to store a JWT token in local storage when working with Next.js?

We are considering using Next.js for our application, with a focus on client-side rendering for data fetching. The API we will be interacting with is external and requires authentication to access specific user dashboard content. While the homepage will ...

Utilize a variable function in AngularJS

Within my app.js file, I have declared functions in the following manner: var func1 = function(v1,v2,v3) { . . } var func2 = function(v1,v2,v3) { . . } Moving on to my controller.js file: var action = ""; if(..) { action = 'func1';} ...

What techniques can be used to maintain the value of 'this' when utilizing async.apply?

Employing async.parallel to simultaneously run 2 functions, initiated from a static function within a mongoose model. In this code snippet (where the model contains a static function named verifyParent), I utilize this to access the model and its functions ...

The issue with AngularJS Factory values not updating across multiple views

My current approach involves a simple factory that retrieves a list of products from a database and allows users to toggle whether a product is marked as a favorite or not through user interactions. function factoryProduct($http,$filter) { var data = { ...

Inconsistent CSS3 transitions behavior in Firefox 4

I have been playing around with some css3 transitions and created a slider test. It works perfectly in webkit browsers, but I have noticed an issue in Firefox 4. When clicking on the left link for the first time, the slider is supposed to slide to the left ...

The Ubiquity CmdUtils.setSelection function does not support replacing HTML code

I'm working on a Ubiquity command to replace selected links or URLs pointing to images with the actual image itself. However, I've found that the CmdUtils.setSelection() function doesn't work when there are html tags in the selection, render ...

Get data from a JSON file using JavaScript

Dealing with a rather intricate and detailed JSON structure, I have a specific extraction task at hand. The goal is to retrieve information (text) from the JSON only if the resource-id includes the text "com.shazam.android:id/" and certain prope ...

Maximizing the Efficiency of jQuery and Javascript Frameworks

Currently, I am working on a project that involves utilizing a JavaScript framework (jQuery) in conjunction with various plugins (validation, jquery-ui, datepicker, facebox, and more) to enhance the functionality of a modern web application. However, I ha ...

A step-by-step guide to incorporating VeeValidate with vue-i18n

When a click event is triggered, I am able to change the language in vue-i18n. However, I am facing an issue with changing the vee-validate dictionary to match the same language. Main.js import VeeValidate from 'vee-validate' import validations ...

Revamp the switch-case statement in JavaScript code

Is there a way to refactor this code in order to prevent repeating dailogObj.image? I would have used a return statement if it wasn't for case 5 where two assignments are required. getDialogData(imageNum): any { const dailogObj = { image: ...

What could be causing the sluggishness of this ajax call?

After implementing an ajax function on the page load event, I noticed that there was no record of the call in the server log (which logs all entrance and exit points of all MVC server methods). The request from the JavaScript to the server was taking an un ...

Developing jasmine tests for an Angular directive

When implementing the directive below, it will check and load a template with either "pass", "fail", or "required" as values based on certain conditions. if(parent value == required){ 1. if the value is true -> $scope.msg = "fail" -> loads the templ ...

Facing difficulty observing Content-Disposition header in create-react-app project

I am using a create-react-app that is being served by express. Express is acting as a proxy for the app and all the application logic resides in CRA. My issue lies in calling an API to download a file. The API sends back a "Content-Disposition" header wit ...

Corporate firewall causing issues with AJAX call execution

Currently, I am utilizing jQuery's $.ajax() method to retrieve approximately 26KB of JSONP data. All major browsers including FF, Chrome, IE, and Safari are successfully returning the data from various locations such as work, home, and mobile phone w ...

Newbie in JavaScript - reinitiating my for loop journey

After running an animation in 4 steps, I want it to restart once all the steps are completed. var aSteps = [ { "x": "800", "y": "0" }, { "x": "800", "y": "500" }, { "x": "0", "y": "500" ...

Switching between fixed and unfixed divs causes two absolute divs to alternate

I am currently working on a code to keep a fixed div ("two") in place between two absolute positioned divs ("one" and "footer") while scrolling. However, there is an issue that arises when the browser window is resized. The distance between the footer and ...

Iterating through every image displayed on a webpage

Two inquiries: What is the best way to efficiently loop through every image on a specific webpage and open each one in a new browser tab? Similar concept, but instead of opening in a new tab, I am interested in substituting different images for the ...

Convert CSV into an object with additional attribute

I'm attempting to import data from a CSV file into a d3 tree graph. While I've successfully loaded the JSON version of the data, the d3.csv parser isn't returning the expected string. Some approaches I've tried include: treeData.forEa ...

Is it possible to swap images by clicking on them?

Let's say I'm working with 3 images. Image A, B and C. A is the main image initially. If I click on image B, it will become the main image and image A will take its place in the old position. The same condition applies when B is the main image a ...