gpt4 book ai didi

html - 获取 Angular js 中返回的 JSON 对象的值

转载 作者:太空宇宙 更新时间:2023-11-04 02:18:21 25 4
gpt4 key购买 nike

我是 Angular 和 Node.js 新手。

基本上,我有一个项目表,每行都有一个按钮单击。如果我点击编辑按钮,详细信息应该出现在相应的文本框中进行编辑。

router.route('/contacts/getone/:pk')
.get(function(req, res, next){
pg.connect(connectionString, function(err, client, done) {
//var id = {pk:req.body.pk};
var contacts = [];
console.log("get1");
console.log(req.params.pk);
var query = client.query("select * from contacts where pk=($1)", [req.params.pk]);
query.on('row', function(row) {
contacts.push(row);
});
query.on('end', function() {
done();
return res.json(contacts);
});
});
});

这是我的 Angular Controller 代码。

$scope.editContact = function(id) {
contact.getone(id)
.success(function(data) {

$scope.contactlist = data;
//console.log("geone");
//document.getElementById("fname").value = $scope.contactlist.firstname;
//console.log("getall");
//console.log($scope.contactlist.firstname);
//console.log(data["firstname"]);
$scope.formData.firstname = $scope.contactlist.firstname;
$scope.formData.lastname = $scope.contactlist.lastname;
$scope.formData.address = $scope.contactlist.address;
$scope.formData.contact = $scope.contactlist.contact;
// $scope.loading = false;
});
};

谁能告诉我如何访问我的node.js代码返回的数据中的值?我的 Angular 中的代码返回未定义的值,例如 $scope.contactlist.firstname 未定义。

这是我执行 console.log(data);

时的图像

demonstration

最佳答案

@Rayin Dabre----这是我的 Controller 的代码。

angular.module('ContactsControl', [])

.controller('contactController', ['$scope', '$http', 'contact', function($scope, $http, contact) {
$scope.formData = {};

contact.get()

.success(function(data) {
console.log("getall");
$scope.contactlist = data;
// $scope.loading = false;
});
$scope.editContact = function(id) {
contact.getone(id)
.success(function(data) {

$scope.contactlist = data;
//console.log("geone");
//document.getElementById("fname").value = $scope.contactlist.firstname;
console.log(data);
//console.log($scope.contactlist.firstname);
//console.log(data["firstname"]);
$scope.formData.firstname = $scope.contactlist.firstname;
$scope.formData.lastname = $scope.contactlist.lastname;
$scope.formData.address = $scope.contactlist.address;
$scope.formData.contact = $scope.contactlist.contact;
// $scope.loading = false;
});
};

关于html - 获取 Angular js 中返回的 JSON 对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34714421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com