我试图从json中获取一个值并将其放入我的控制器并出现问题。
我希望将会员值8放入$ scope.value = data.membership;
JS。服务电话
.service('getMembership', function ($http, SERVER_URL) {
return {
getMembership: function (userid) {
var url = SERVER_URL + "services/getMembership.php?userid=" + userid;
return $http.get(url);
}
};
})
php将返回以下内容,而javascript将返回该内容。
{"membership":"8"}
现在我试图在我的控制器中添加它并将其调用到$ scope.value中的图表中。
.controller('AwardCtrl', function ($scope, $interval, getMembership) {
var userId = 1;
getMembership.getMembership(userId).success(function(data) {
$scope.value = data.membership;
$scope.options1 = {
animate : { enabled: true, duration: 1500, ease: 'bounce' },
readOnly: true,
size: 200,
max: 30,
subText: {
enabled: true,
text: 'DAYS LEFT',
color: '#f2f2f2;',
font: 'auto'
},
trackWidth: 40,
barWidth: 25,
trackColor: '#e6e6e6',
barColor: '#81d8cf',
dynamicOptions: true
}
});
})
答案 0 :(得分:1)
PHP将返回一个字符串,但是javascript $ http.get会自动将其转换为JSON对象吗?如果没有,您可以使用
var actualobject = JSON.parse(data);
$scope.value = actualobject.membership;
转换它。请参阅:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse