gpt4 book ai didi

javascript - Angular 工厂$resource在数据库中创建一个空对象

转载 作者:行者123 更新时间:2023-12-03 06:34:37 28 4
gpt4 key购买 nike

我已经使用 Angular $resource 创建了一个 REST 应用程序,但我注意到每次我使用表单进行放置或发布时...每次重新加载页面时,$resource 都会在数据库中创建一个空条目(对象),然后显示在前端。当未进行发布/放置而只是重新加载时,也会发生这种情况。我不知道我是否正确使用了 $save 函数,或者我的代码是否有问题

==========================HTML====================== ===================

<div id="hello">
<div class='row'>
<div class="col-md-6">
<label for="content">Get greeting by ID:</label>
<div>
<input type='text' ng-model='$ctrl.getId' name="id" class="form-control"/>
<span> {{ $ctrl.greeting.content }} </span>
</div>
<button ng-click='$ctrl.get();'>Get</button>
<hr/>
<div class="form-group">
<label for="content">Post new Greeting:</label>
<div>
<textarea ng-model='$ctrl.postData' name="content" class="form-control"></textarea>
</div>
<button ng-click='$ctrl.post();'>Submit</button>
</div>
<hr/>
<div class="form-group">
<label for="content">Edit greeting by ID:</label>
<div>
<input type='text' ng-model='$ctrl.updateId' name="id" class="form-control"/>
</div>
</div>
<div class="form-group">
<div>
<textarea ng-model='$ctrl.updateData' name="content" class="form-control"></textarea>
</div>
<button ng-click='$ctrl.put();'>Submit</button>
</div>
<hr/>
</div>
<div class="col-md-6">
<hr/>
<div class='row' ng-init="$ctrl.listAll()">
<div ng-repeat="eachElement in $ctrl.greetingList.hello_collection">
<div class="col-md-2"> {{ eachElement.id }} </div>
<div class="col-md-5"> {{ eachElement.content }} </div>
<div class="col-md-5"><button ng-click="$ctrl.remove(eachElement.id)">delete</button></div>
</div>
</div>
</div>
</div>
<hr/>

==============================工厂================== ===========

.factory("helloService", ['$resource',
function($resource) {
return $resource('/direct/hello/:greetingId.json',
{ greetingId: '@greetingId' }, {
get: {
method: 'GET',
params: {greetingId: '@greetingId'},
isArray: false
},
post: {
method: 'POST',
headers:{'Content-Type':'application/json; charset=UTF-8'},
},
put: {
method: 'PUT',
params: {greetingId: '@greetingId'}
},
remove: {
method: 'DELETE',
params: {greetingId: '@greetingId'}

},
listAll: {
method: 'GET',
isArray: false
},
});
}
])

==================================组件============ ===========

.component('hello', {
controller: ['helloService',
function (helloService) {
var self = this;
self.greetingList;
function get() {
self.greeting = helloService.get({greetingId: self.getId});
}
self.get = get;

function post() {
self.newGreeting = helloService.post({'content': self.postData }, function(data){
console.log(data);
});
self.greetingList.hello_collection.push(self.newGreeting);
self.postData = "";
}
self.post = post;

function put() {
helloService.put({greetingId: self.updateId}, {'content': self.updateData });
var newValue = self.updateData;
var greeting = helloService.get({greetingId: self.updateId}, function(data){
data.content == newValue;
console.log(data);
console.log(data.content);
});
console.log(greeting);
/**
for (var i = 0; i < self.greetingList.hello_collection.length; i++)
{
if(self.greetingList.hello_collection[i].greetingId == self.updateId){
console.log(self.greetingList.hello_collection[i]);
self.greetingList.hello_collection[i].content = self.updateData;
}
}
**/
self.updateId, self.updateData = "";
}
self.put = put;

function remove(deleteId) {
var greeting = helloService.get({greetingId: deleteId})
for (var i = 0; i < self.greetingList.hello_collection.length; i++) {
if (self.greetingList.hello_collection[i]["id"] == deleteId) {
var index = i;
break;
}
}
if (index >= 0) {
self.greetingList.hello_collection.splice(index, 1);
helloService.remove({greetingId: deleteId}, function() {
greeting.$delete();
});
}
}
self.remove = remove;
var greetingList;
function listAll() {
self.greetingList = helloService.listAll();
}
self.listAll = listAll;
var newService = new helloService();
newService.$save();

}
],
templateUrl: "./templates/hello.html",

最佳答案

我已经更新了您的组件和服务以使用 resource.$save() 和 $resource.remove(),如下所示:

更新服务

.factory("helloService", ['$resource',
function($resource) {
return $resource('/direct/hello/:greetingId.json', {
greetingId: '@greetingId'
}, {
get: {
method: 'GET',
params: {
greetingId: '@greetingId'
},
isArray: false
},
post: {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
},
put: {
method: 'PUT',
params: {
greetingId: '@greetingId'
}
},
remove: {
method: 'DELETE',
params: {
greetingId: '@greetingId'
}

},
listAll: {
method: 'GET',
isArray: true // <-- this should be true as in a RESTFULL app the
// listAll should expect array of objects as a
// response i.e.: [{id:1,content:"..."},
// {id:2,content:"..."}]
},
});
}
]);

更新组件

.component('hello', {
controller: ['helloService',
function (helloService) {
var self = this;

function get() {
self.greeting = helloService.get({greetingId: self.getId});
}
self.get = get;

function post() {
var greetingItem = new helloService();
greetingItem.content = self.postData;
greetingItem.$save();
self.greetingList.push(greetingItem);
self.postData = "";
}
self.post = post;

function put() {
var greetingItem = helloService.get({greetingId: self.updateId}, function() {
greetingItem.content = self.updateData;
greetingItem.$save();
console.log(greetingItem);
self.updateId = "";
self.updateData = "";
});

}
self.put = put;

function remove(deleteId) {
var greetingItem = helloService.get({greetingId: deleteId}, function(){
for (var i = 0; i < self.greetingList.length; i++) {
if (self.greetingList[i].id == deleteId) {
var index = i;
break;
}
}
greetingItem.$delete({greetingId: deleteId});
if (index >= 0) {
self.greetingList.splice(index, 1);
}
});
}
self.remove = remove;

var greetingList = [];
function listAll() {
self.greetingList = helloService.listAll();
}
self.listAll = listAll;

}
],
templateUrl: "./templates/hello.html"
});

关于javascript - Angular 工厂$resource在数据库中创建一个空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38285304/

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