Exploring AngularJS: Effortlessly Retrieving Object Values

HTML:

  <div ng-app = "" ng-controller = "personController">
     <p>Persoon: 
        <select ng-model="persoon">
           <option ng-repeat="person in persons">{{person.FirstName}} {{person.FamilyName}}</option>
        </select>
     </p>
     <p>De geboortedatum van <b>{{persoon}}</b> is {{persoon.BirthDate}} en is dus 16 jaar</p>
  </div>

  <script>
     function personController($scope,$http) {
        var persons = "persons.txt";
        var persoon = "";
        $http.get(persons).then( function(response) {
           $scope.persons = response.data;
        });
     }
  </script>

JSON:

[
   {
      "FirstName" : "Harry",
      "FamilyName" : "James",
      "BirthDate" : "29-10-1920"
   }

]

Display the BirthDate of the selected person from the dropdown list. Need help with making it work correctly, as I've tried various approaches without success. Any assistance would be greatly appreciated.

Answer №1

To set the value, use person.BirthDate

 <option  value="{{person.BirthDate}}"  ng-repeat="person in persons">{{person.FirstName}} {{person.FamilyName}}</option>

SAMPLE

 var app = angular.module('myapp',[]);
 app.controller('personController',function($scope){
 $scope.persons = [
   {
      "FirstName" : "Harry",
      "FamilyName" : "James",
      "BirthDate" : "29-10-1920"
   }

];
 });
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app = "myapp" ng-controller = "personController">
     <p>Person: 
        <select ng-model="person">
           <option  value="{{person.BirthDate}}"  ng-repeat="person in persons">{{person.FirstName}} {{person.FamilyName}}</option>
        </select>
     </p>
     <p>The birth date of <b>{{person}}</b> is {{person.BirthDate}} and he/she is now 16 years old</p>
  </div>

UPDATE

It's advisable to utilize ng-options over ng-repeat.

SAMPLE

var app = angular.module('myapp',[]);
 app.controller('personController',function($scope){
 $scope.persons = [
   {
      "FirstName" : "Harry",
      "FamilyName" : "James",
      "BirthDate" : "29-10-1920"
   }

];
 });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app = "myapp" ng-controller = "personController">
     <p>Person: 
        <select ng-model="person" ng-options="n.FirstName + ' ' + n.FamilyName for n in persons"></select>
     </p>
     <p>The birth date of <b>{{person}}</b> is {{person.BirthDate}} and he/she is now 16 years old</p>
  </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

Trouble arises when attempting AJAX call to PHP file resulting in failed MySQL execution accompanied by errors

Having some trouble identifying the issue here. It seems quite simple, but I'll highlight it here anyway. On a basic website, I have a form for user registration where I'm using AJAX to send data to register.php. The form and AJAX code are as fol ...

What steps can be taken to automatically scroll to an element in a Protractor test when the element cannot be found within the view?

When running my suite of tests, I want Protractor to automatically scroll to elements on the page if it is unable to find them in the current view. Some recent UI changes resulted in a few end-to-end tests failing, as the headers were made fixed. Current ...

The output of jquery's val() function will not show the actual value

Is it proper to use html() for setting content in non-form elements like divs? This question has come up a few times, which I wasn't aware of. I've been working on setting a value after fetching comments through a $.ajax call. When I check the v ...

Issue with passing reactive property to component in Vue 3 application

I am currently working on a Vue 3 application and I am in the process of setting up a store for state management. Within this application, I have several important files that play different roles: app.vue component.vue main.js store.js These files contai ...

What strategies can be implemented to improve the load time for bigvideo.js?

Why are my load times extremely slow for such a small site? I had hoped to display an image before the video loads, but they both seem to load simultaneously, even if the video takes forever to load. This is my HTML: <div id="bigvid"> <div cla ...

Checkbox Selection - Modify Prompt to "Kindly mark this box to continue"

I have created a multi-select checkbox form, and I've added some JavaScript to ensure that the visitor selects at least one option <div class="form-group options"> <label class="control-label col-md-4" for="optiontext">Specify an option&l ...

Information disappearing upon returning to a previous screen

Currently, I am developing a web application using AngularJS and HTML. As part of the application, I created a dashboard on the home screen (referred to as Screen A) that displays a list of clients with a summary. When a client is clicked, it navigates t ...

Oops! Something went wrong: [$compile:ctreq] The controller 'ngModel' needed for the directive 'ngShow' cannot be located

Hello, I recently started working with AngularJS and I am looking to execute a function when the submit button is clicked. Here is the HTML code from my page: <div class="form-group"> <a href="#" ng-click="showGraph = !showGr ...

Dynamic loading and endless scrolling in jQuery with advanced filtering functionality

I have a page full of items that I filter by class names. This works great, but when I added lazy loading using ajax, it started causing some issues. Currently, I load the next page of items without applying the filter to the ajax call, and then hide the ...

The functionality of Inettuts within JavaScript seems to malfunction whenever I attempt to reposition the div

I developed a widget framework using inettuts and integrated database functionalities with ajax in asp.net and sqlserver. Widgets are dynamically loaded based on user data from the database, but I encountered an issue when trying to move a widget - the J ...

Updating elements within an array on the fly

In this scenario, let's consider two arrays: array A = [2, 5, 6] and array B = [1, 2, 3, 4, 5, 6, 7, 8]. The values of array B are initially hidden within boxes and become revealed when clicked. The goal is to replace certain values in array B with un ...

Discover the complete file path using node.js incorporating the express-busboy module

I am currently utilizing node.js and express-busboy for the purpose of uploading a file via a file input form to my server. Once uploaded, the file will be stored in a directory with a path resembling something like root/useruploaded/formattached/somerando ...

Tips for embedding a file within a text box in HTML and enabling users to make direct edits

I am currently working on a feature that allows users to open a .txt or .html file from their file explorer and paste the contents into a textarea for editing and saving purposes. My question is whether it's possible to read the file content and auto ...

Objects that are included into an array will end up with an undefined value

Currently, I am in the process of coding a command for my Discord bot that involves adding items to an array of objects stored within a JSON file. To achieve this functionality, I have implemented the following code snippet: let rawdata = fs.readFileSync(& ...

Attempt to generate a function in JavaScript that mirrors an existing one

How can I create a javascript function similar to loadAllOriginal, but with the value of the variable allEmployees being a list of employee objects (ID, FirstName, LastName)? I am attempting to implement this method in combination with autocomplete from ...

Text Separation Within a Singular JSON Object

Currently, I am offering my assistance as a volunteer to develop a fast AngularJS App for a non-profit organization. Although I have successfully set up Angular, the main issue I am encountering has to do with parsing JSON data. The JSON object that I am ...

parsing an array using jQuery's getJSON

Finally got the simplified array to work, check it out below If you're still struggling with parsing a complicated array, you can refer to this link. TLDR: I need to extract and insert each heading from an array into a div using Jquery - getJSON, wi ...

Changing the color of placeholder text in MUI 5 TextField

Looking to customize the text color and placeholder text color in my MUI TextField component to be green https://i.sstatic.net/NZmsi.png The documentation doesn't provide clear instructions, so I attempted a solution that didn't work: <TextF ...

What is preventing the action method from receiving parameters from the ajax call?

I am facing an issue with sending the FileHeadNo from ajax to my controller action as it seems the controller is not receiving the FileHeadNo parameter. $(function () { $("#txtReportNo").autocomplete({ source: function (reque ...

Why is the output [[]] instead of [] after removing the last array like [["1"]] using .splice()?

let array=[["1"]]; array[0].splice(0,1); // array = [[]] Why am I unable to make the sub-array blank when removing the last element? I want array = [] instead of [[]] after removing the last element from a sub-array. Check it out here: http://jsbin.com/ ...