Getting a precise item in JSON with varied key signatures

Given the following JSON data:

var responses = {customer : {results: 2938; id: 9283}, bredesh : {results: 2938; id: 248}    };

I am trying to use an if statement in my HTML template :

<div ng-if="response.(customer and bredesh and any new element (Issue is here) ).id=='a'" class="form-group">

Answer №1

If you want to showcase all elements of an object and choose a specific one using ng-if, consider listing the keys and values separately using the syntax

ng-repeat="(key,value) in your_array"
. Here is a brief example:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.responses = {
    customer: {
      results: 2938,
      id: 9283
    },
    bredesh: {
      results: 2938,
      id: 248
    }
  }
  $scope.id = 248;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

  <div ng-repeat="(key, value) in responses">
    <div ng-if="responses[key].id==id" class="form-group">
      {{key}} - {{value}}
    </div>
  </div>
  <button ng-click="id=248">Select ID: 248</button>
  <button ng-click="id=9283">Select ID: 9283</button>

</div>

Alternatively, if manual selection is needed, use responses.customer.id and responses.bredesh.id.

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

Version discrepancy in module metadata

Yesterday everything was running smoothly with Angular 2 and Angular Material, but today I encountered an error when trying to run the program. Can someone please help? ERROR in Error: Metadata version mismatch for module E:/Demo/crud/ node_modules/ ...

Combining arrays using Observables in Typescript with RxJS

Having some issues using rxjs Observable.concat function in typescript. Encountering an error "Cannot read property 'apply' of undefined" The problem appears to be limited to typescript and may be related to rxjs version 5 concat. The code seems ...

Implementing nested resolvers in Angular can be achieved by utilizing the power

One of the components in my project retrieves a category using a resolver. Here's how it's done: ngOnInit() { this.category = this.route.snapshot.data['category']; } It works great and fetches a list of users which make up the cat ...

"Unexpected Type Inference Issue: A variable initially defined as a string inexplicably transforms into 'undefined'

Currently, I am incorporating the await-to-js library for handling errors (specifically utilizing the to method from the library). In an intriguing scenario, the variable type shifts to string | undefined within a for..of loop, whereas outside of the loop ...

AngularJS: Organizing elements across multiple ng-repeats

UPDATE: Here is a link to the plunker I created. In my JSON string, I have 2 parent objects with children. { "ParentA": { ... }, "ParentB": { ... } } Both ParentA and ParentB have children with some overlapping names. I ...

Converting and Casting Enums in TypeScript

Is there a way to convert one enum into another when they have the same values? enum Enum1 { Value = 'example' } enum Enum2 { Value = 'example' } const value = Enum1.Value const value2 = value as Enum2 ...

Issues passing a factory's $http.get response to the controller in AngularJS using $routeProvider

I have a CRUD operation that I want to reuse, so I set up a factory passed to the controller through $routeProvider/resolve. However, the issue is that the controller resolves before the factory's GET request completes. How can I use promises to ensu ...

The ng test option is failing to execute effectively

Attempting to conduct unit tests utilizing Karma and Jasmine through the ng test is proving to be a bit challenging. Upon issuing the following command: ng test --watch=false --code-coverage --main ./src/main/resources/public/scripts/xyz/workspace/commons ...

What methods are recommended for implementing changes in a TypeScript library throughout the development process?

When working on a TypeScript library that is utilized as a dependency by other libraries or applications, how can I efficiently handle frequent updates without going through the process of incrementing the version number, publishing it to an NPM registry, ...

Dealing with an AWS S3 bucket file not found error: A comprehensive guide

My image route uses a controller like this: public getImage(request: Request, response: Response): Response { try { const key = request.params.key; const read = getFileStream(key); return read.pipe(response); } catch (error ...

Reusing Angular routes across different modules for backbutton functionality

Insights on my Application (Angular 12): Comprises of 3 Modules, each containing an overview page with a list and specific detail pages Each route is assigned an area tag to identify the user's navigation within the module Goal for Angular´s RouteR ...

AngularJS: Issue with ng-show and ng-click not functioning when button is clicked

I have a specific requirement where I need to display and hide the description of each column in a table when a button is clicked. Here is the visual representation of what I have: the table In my HTML code, I have defined a button with ng-click as a func ...

Displaying summaries for all items (including those that are not currently visible) in the Kendo UI Grid while paging or grouping

Is it possible to display aggregates for all items in a grid data source while using paging and grouping? In my list of 1000+ items with columns like Description, Type, Cost, I am fetching only 200 items at a time using OData. However, when viewing the ag ...

What is the best way to invoke a Service from a Directive in AngularJS?

Every time a user clicks, a directive is triggered to change the icon for adding to their favorite list. View: <div class="col col-33"> <i class="icon ion-ios-heart-outline" favorite="place[0].objectId" on-icon="ion-ios-heart-outline" off-ic ...

Utilizing a Link element in conjunction with ListItem and Typescript for enhanced functionality

I am currently using material-ui version 3.5.1 My goal is to have ListItem utilize the Link component in the following manner: <ListItem component={Link} to="/some/path"> <ListItemText primary="Text" /> </ListItem> However, when I tr ...

Access-Control-Allow-Origin header not being sent by ExpressJS

In the midst of my project, I find myself needing an angular web application to connect with a node/express backend. Despite trying to implement Cors for this purpose, the express server refuses to send the Access-Control-Allow-Origin header. I am perplexe ...

Unable to grab hold of specific child element within parent DOM element

Important Note: Due to the complexity of the issue, the code has been abstracted for better readability Consider a parent component structure like this: <child-component></child-component> <button (click)="doSomeClick()"> Do Some Click ...

ngx-bootstrap encountered an issue: TypeError - _co.openModal is unavailable as a function

Just starting out with ngx-bootstrap, I was attempting to create a modal using the instructions from this site : However, I encountered the following error: ERROR TypeError: _co.openModal is not a function Here are some code snippets: //app.component ...

Issue with displaying bound value in AngularJS textbox

Hello! I am currently working on an AngularJS application and have encountered a puzzling issue. I am attempting to bind a value to a textbox using the following code: <ul> <li ng-repeat="screen in screenMap"> <input type="text" ...

angular2-highcharts series highlighting feature

I am working with an angular2-highcharts chart and I am trying to highlight specific lines of a data series when clicked. However, when using the code below, I encounter an error message that says Cannot read property 'series' of undefined. T ...