I'm quite new to angularjs and somehow theres something I can't make a workaround.
我对angularjs很新,不知何故,我无法解决这个问题。
Its getting a single value from a json array and then placing it in a scope variable. Lets say just got an array from a php query which looks like this (printed view):
它从json数组中获取单个值,然后将其放入范围变量中。让我们说从一个php查询得到一个数组看起来像这样(打印视图):
array(
[name] => John Doe,
[age] => 29,
[gender] =>male);
And then it is added through this scope variable:
然后通过此范围变量添加:
$scope.profile = response.data;
How can I pick a single a value from that array and then place it separate scope variables?
如何从该数组中选择一个值,然后将其放在单独的范围变量中?
$scope.name; $scope.age; $scope.gender;
2 个解决方案
#1
1
Send your data back from your PHP using json_encode
使用json_encode从PHP发回数据
return json_encode($array);
which will then give you
然后会给你
object {
name: 'John Doe',
age: '29',
gender: 'male'
}
And you can then access it using:
然后您可以使用以下方式访问它:
$scope.profile = response.data;
//
$scope.name = $scope.profile.name;
$scope.age = $scope.profile.age;
$scope.gender = $scope.profile.gender;
Example:
例:
var app = angular.module('someApp', [])
.controller('someCtrl', function ($scope) {
var object = {
name: 'John Doe',
age: '29',
gender: 'male'
};
$scope.profile = object;
$scope.name = $scope.profile.name;
$scope.age = $scope.profile.age;
$scope.gender = $scope.profile.gender;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="someApp" ng-controller="someCtrl">{{name}}, {{age}}, {{gender}}</div>
#2
0
$scope.name = response.data.name;
$scope.age = response.data.age;
$scope.gender = response.data.gender;
This should add them to the $scope
.
这应该将它们添加到$ scope。
Make sure that the elements are in an object, not JSON string anymore.
确保元素在对象中,而不再是JSON字符串。
Or if you stay with the other one you already have:
或者,如果你和另一个人在一起,你已经拥有:
$scope.name = $scope.profile.name;
$scope.age = $scope.profile.age;
$scope.gender = $scope.profile.gender;
#1
1
Send your data back from your PHP using json_encode
使用json_encode从PHP发回数据
return json_encode($array);
which will then give you
然后会给你
object {
name: 'John Doe',
age: '29',
gender: 'male'
}
And you can then access it using:
然后您可以使用以下方式访问它:
$scope.profile = response.data;
//
$scope.name = $scope.profile.name;
$scope.age = $scope.profile.age;
$scope.gender = $scope.profile.gender;
Example:
例:
var app = angular.module('someApp', [])
.controller('someCtrl', function ($scope) {
var object = {
name: 'John Doe',
age: '29',
gender: 'male'
};
$scope.profile = object;
$scope.name = $scope.profile.name;
$scope.age = $scope.profile.age;
$scope.gender = $scope.profile.gender;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="someApp" ng-controller="someCtrl">{{name}}, {{age}}, {{gender}}</div>
#2
0
$scope.name = response.data.name;
$scope.age = response.data.age;
$scope.gender = response.data.gender;
This should add them to the $scope
.
这应该将它们添加到$ scope。
Make sure that the elements are in an object, not JSON string anymore.
确保元素在对象中,而不再是JSON字符串。
Or if you stay with the other one you already have:
或者,如果你和另一个人在一起,你已经拥有:
$scope.name = $scope.profile.name;
$scope.age = $scope.profile.age;
$scope.gender = $scope.profile.gender;