[英]AngularJS how to access values of objects
HTML: 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: JSON:
[
{
"FirstName" : "Harry",
"FamilyName" : "James",
"BirthDate" : "29-10-1920"
}
]
So I have a select box where you are able to select a person.所以我有一个选择框,您可以在其中选择一个人。 What I want is to display that persons BirthDate.
我想要的是显示那个人的出生日期。 I don't know how to get it to work I've tried several things but nothing is working.
我不知道如何让它工作我已经尝试了几件事,但没有任何效果。 Please Help
请帮忙
You need to set the value as person.BirthDate
您需要将该值设置为
person.BirthDate
<option value="{{person.BirthDate}}" ng-repeat="person in persons">{{person.FirstName}} {{person.FamilyName}}</option>
DEMO演示
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>Persoon: <select ng-model="persoon"> <option value="{{person.BirthDate}}" 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>
EDIT编辑
It is always better to use ng-options instead of ng-repeat.使用 ng-options 而不是 ng-repeat 总是更好。
DEMO演示
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>Persoon: <select ng-model="persoon" ng-options="n.FirstName + ' ' + n.FamilyName for n in persons"></select> </p> <p>De geboortedatum van <b>{{persoon}}</b> is {{persoon.BirthDate}} en is dus 16 jaar</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.