Is there a convenient way to read key/value from an object with only one key in AngularJS expression? Below is an example.
<div ng-app="app" ng-controller="ctrl">
<span>{{me.key}}</span> //I want: <span>name</span>
<span>{{me.value}}</span> //I want: <span>Ealon</span>
</div>
angular.module("app", [])
.controller("ctrl", function($scope) {
$scope.me = {
"name": "Ealon" //This object contains only one key
};
})
Using ng-repeat
to display the key/value pair is a solution. But unfortunately, I cannot use ng-repeat
here for some reason.
Another solution, which I think is not convenient, is:
<div ng-app="app" ng-controller="ctrl">
<span>{{meCopy.key}}</span>
<span>{{meCopy.value}}</span>
</div>
angular.module("app", [])
.controller("ctrl", function($scope) {
$scope.me = {
"name": "Ealon"
};
var meKey = Object.getOwnPropertyNames($scope.me)[0];
var meValue = $scope.me[meKey];
$scope.meCopy = {
"key": meKey,
"value": meValue
};
})
Is there any simpler way to do this? Thanks for your help!