我正在研究一个卑鄙的堆栈问题,并且我已经完全正常运行了,但我仍然试图从表单字段中提取实际数据。我目前的数组值是硬编码的,但是我想用输入的表单字段值替换'edited'。
我的角度PUT请求:
$scope.editService = function(id) {
$http.put('/api/hc/' + id,
{title: 'edited',
shortname: 'edited',
summary: 'edited',
description: 'edited'}
)
.success(function(data) {
})
.error(function(data) {
console.log('Error: ' + data);
});
};
我的角色形式:
<form name="editForm" ng-submit="editService(service._id)" ng-repeat="service in services | filter:json">
<input type="text" placeholder="{{ service.title}}" ng-model="serviceTitle" required>
<input type="text" placeholder="{{ service.shortname}}" ng-model="serviceShortname" required>
<input type="text" placeholder="{{ service.description}}" ng-model="serviceSummary" required>
<textarea type="text" placeholder="{{ service.summary}}" ng-model="serviceDescription" required></textarea>
<button type="submit">Edit</button>
</form>
我最初的想法是用$ scope.serviceTitle替换'编辑'的值,但这似乎不起作用。有效负载返回空。
答案 0 :(得分:0)
我通过将值作为参数传递出去,如下所示。
$scope.editService = function(id, ok, shortname, summary, descrip) {
$http.put('/api/hc/' + id,
{title: ok,
shortname: shortname,
summary: summary,
description: descrip}
)
.success(function(data) {
})
.error(function(data) {
console.log('Error: ' + data);
});
};
我的表格:
<form name="editForm" ng-submit="editService(service._id, title, shortname, summary, descrip)" ng-repeat="service in services | filter:json">
<input type="text" placeholder="{{ service.title}}" ng-model="title" required>
<input type="text" placeholder="{{ service.shortname}}" ng-model="shortname" required>
<input type="text" placeholder="{{ service.description}}" ng-model="summary" required>
<textarea type="text" placeholder="{{ service.summary}}" ng-model="descrip" required></textarea>
<button type="submit">Edit</button>
</form>
答案 1 :(得分:0)
我认为下面是正确的方法,我假设您在以前的代码中填充$ scope.service。注意我删除了占位符,并使用$ scope.service作为ng-model变量。
$scope.submit = function() {
$http.put('/api/hc/' + $scope.service.id, $scope.service)
.success(function(data) {
})
.error(function(data) {
console.log('Error: ' + data);
});
};
<form name="editForm" ng-submit="submit()" ng-repeat="service in services | filter:json">
<input type="text" ng-model="service.title" required>
<input type="text" ng-model="service.shortname" required>
<input type="text" ng-model="service.description" required>
<textarea type="text" ng-model="service.summary" required></textarea>
<button type="submit">Edit</button>
</form>