Javascript 如何在输入中放置响应
我想把收到的数据输入我的3个输入。我从节点服务器接收json响应,控制器应该接收响应并将其放入我的3个输入中。 我收到一个响应,但我无法将其放入输入中 控制器:Javascript 如何在输入中放置响应,javascript,angularjs,node.js,Javascript,Angularjs,Node.js,我想把收到的数据输入我的3个输入。我从节点服务器接收json响应,控制器应该接收响应并将其放入我的3个输入中。 我收到一个响应,但我无法将其放入输入中 控制器: $scope.edit = function(id, contact) { console.log(id); $http.get('/contactlist/' + id).then(function(response) { console.log(response); $scope.contact = response; })
$scope.edit = function(id, contact) {
console.log(id);
$http.get('/contactlist/' + id).then(function(response) {
console.log(response);
$scope.contact = response;
});
};
app.get('/contactlist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
connection.query('SELECT * FROM contactlist WHERE id = ' + id, function (error, results, fields) {
console.log(results);
res.json(results);
});
});
<div class="input-field col s4">
<input id="name" type="text" class="form" ng-model="contact.name">
<label for="name">Nom</label>
{{contact.name}}
</div>
<div class="input-field col s4">
<input id="email" type="text" class="form" ng-model="contact.email">
<label for="email">Email</label>
</div>
<div class="input-field col s4">
<input id="number" type="text" class="form" ng-model="contact.number">
<label for="number">Numéro</label>
</div>
服务器:
$scope.edit = function(id, contact) {
console.log(id);
$http.get('/contactlist/' + id).then(function(response) {
console.log(response);
$scope.contact = response;
});
};
app.get('/contactlist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
connection.query('SELECT * FROM contactlist WHERE id = ' + id, function (error, results, fields) {
console.log(results);
res.json(results);
});
});
<div class="input-field col s4">
<input id="name" type="text" class="form" ng-model="contact.name">
<label for="name">Nom</label>
{{contact.name}}
</div>
<div class="input-field col s4">
<input id="email" type="text" class="form" ng-model="contact.email">
<label for="email">Email</label>
</div>
<div class="input-field col s4">
<input id="number" type="text" class="form" ng-model="contact.number">
<label for="number">Numéro</label>
</div>
index.html:
$scope.edit = function(id, contact) {
console.log(id);
$http.get('/contactlist/' + id).then(function(response) {
console.log(response);
$scope.contact = response;
});
};
app.get('/contactlist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
connection.query('SELECT * FROM contactlist WHERE id = ' + id, function (error, results, fields) {
console.log(results);
res.json(results);
});
});
<div class="input-field col s4">
<input id="name" type="text" class="form" ng-model="contact.name">
<label for="name">Nom</label>
{{contact.name}}
</div>
<div class="input-field col s4">
<input id="email" type="text" class="form" ng-model="contact.email">
<label for="email">Email</label>
</div>
<div class="input-field col s4">
<input id="number" type="text" class="form" ng-model="contact.number">
<label for="number">Numéro</label>
</div>
笔名
{{contact.name}
电子邮件
努梅罗
在chrome上收到的响应:
使用$http服务时,
then
函数中解析的对象包含完整响应。响应对象不仅包含您的数据,它还包含属性,如statusCode和发送请求时使用的配置。因此,您要做的是:
$http.get('/contactlist/' + id).then(function(response) {
console.log(response);
$scope.contact = response.data;
});
请注意response.data的用法,它比response
更具评分性。另外,请注意,在服务器端,您可能只想返回第一个结果,而不是结果数组:
app.get('/contactlist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
connection.query('SELECT * FROM contactlist WHERE id = ' + id, function (error, results, fields) {
console.log(results);
res.json(results[0]);
});
});
您的响应包含带有对象的数据数组。数据[0]具有联系人对象
$http.get('/contactlist/' + id).then(function(response) {
$scope.contact = response.data[0];
});
既然您收到了联系人列表,我建议您这样做:
$http.get('/contactlist/' + id).then(function(response) {
console.log(response);
$scope.contacts = response.data; //note the 's'
});
和html格式
<div ng-repeat="contact in contacts">
<div class="input-field col s4">
<input id="name" type="text" class="form" ng-model="contact.name">
<label for="name">Nom</label>
{{contact.name}}
</div>
<div class="input-field col s4">
<input id="email" type="text" class="form" ng-model="contact.email">
<label for="email">Email</label>
</div>
<div class="input-field col s4">
<input id="number" type="text" class="form" ng-model="contact.number">
<label for="number">Numéro</label>
</div>
</div>
笔名
{{contact.name}
电子邮件
努梅罗
console.log打印什么?当您记录时,response
是什么样子?它可能应该是$scope.contact=response.data
Change$scope.contact=response;至$scope.contact=response.data@tymeJV我给了你一个截图,我编辑了帖子,建议你改变服务器端,只返回一个结果,而不是一个数组。当端点包含一个ID时,这是一般的约定。这是假设您自然地只期望来自db的一个结果。很好,谢谢,问题解决了!非常感谢您的回复。我有一个指示:ng repeat=“联系人列表中的联系人”,因为我不需要重复输入。我重复一下:{{contact.name}&.email&.numberI接受您的回复,因为这是我实际使用的解决方案,您帮助我理解了数据对象接收,感谢您,没有问题。很高兴这有帮助。