我创建了一个可以存储项目的页面。但是当我尝试添加一个项目并在创建后立即删除它时,id是未定义的。当我在插入项目后刷新页面时,我可以毫无问题地删除该项目。
我需要找到一种方法,在我将洞穴内容推送到json数组之前,我可以从数据库中获取项目ID。
<form ng-submit="submit('Bedrijf')">
<input type="text" ng-model="title" placeholder="Add item" />
<input type="text" ng-model="searchText" placeholder="Search for item"/>
<button type="submit">Submit</button>
</form>
<br/>
<ul>
<li ng-repeat="project in projects | orderBy:'id':true | filter:statusFilterOpen | filter:searchText | limitTo: 10">
<a href="projects/[[project.id]]">[[project.title]]</a>
<a class="btn" ng-click="remove(project.id)">Delete</a>
</li>
</ul>
project.controller("PrjctCtrl", function($scope, $http, $timeout) {
$http.get('js/projects.json').
success(function(data, status, headers, config) {
$scope.projects = data;
$scope.statusFilterOpen = function (project) {
return project.status < 100;
};
$scope.statusFilterClosed = function (project) {
return project.status === 100;
};
}).
error(function(data, status, headers, config) {
// log error
});
$scope.submit = function($company) {
data = {
title: $scope.title,
company: $company,
status: 0
};
$http.post('add-projects', data).success(function(post) {
console.log(post)
$scope.projects.push(post);
});
};
$scope.remove = function(id) {
$http.post('delete-project/' + id).success(function(test) {
$scope.projects.splice($scope.projects.indexOf(id),1);
if(popupService.showPopup('Really delete this?')){
movie.$delete(function(){
$window.location.href='';
});
}
});
}
});
我使用laravel将所有内容存储在数据库中。
public function store()
{
$content = Input::json()->all();
DB::table('projects')->insert(
$array = array(
'title' => $content['title'],
'active' => '1',
'company' => 'Bedrijf',
'id' => 'test'
)
);
return $array;
}
public function destroy($id) {
$project = Projects::find($id);
$project->delete();
return 'ok';
}
答案 0 :(得分:0)
只是查看你的代码,delete语句的格式与其他语句的格式不同。
<a href="projects/[[project.id]]">[[project.title]]</a>
<a class="btn" ng-click="remove(project.id)">Delete</a>
应该是:
<a href="projects/[[project.id]]">[[project.title]]</a>
<a class="btn" ng-click="remove([[project.id]])">Delete</a>