Javascript 如何让angular js$http.delete请求在图像应用程序中工作
嗨,我有一个图像相册应用程序,它正在从jsonplaceholder.com获取数据 我编写了一个$http.delete()方法,但它似乎没有按照提供的索引删除照片 以下是html:Javascript 如何让angular js$http.delete请求在图像应用程序中工作,javascript,angularjs,http,http-delete,Javascript,Angularjs,Http,Http Delete,嗨,我有一个图像相册应用程序,它正在从jsonplaceholder.com获取数据 我编写了一个$http.delete()方法,但它似乎没有按照提供的索引删除照片 以下是html: <div ng-app="myApp" ng-controller="AlbumCtrl"> <div class="bar"> <h3>Search by ID or Title</h3>&l
<div ng-app="myApp" ng-controller="AlbumCtrl">
<div class="bar">
<h3>Search by ID or Title</h3><input type="text" class="search" ng-model="q" placeholder="Enter your search terms" />
</div>
<div ng-repeat="album in albumData|filter:q" id="thumbWrapper">
<h1>{{album.id}}</h1>
<h4>Title: </h4><button class="show" ng-click="showme = !showme">{{album.title}}</button>
<div id="thumbList"ng-show="showme"class="albumContent">
<ul ng-controller="PhotoCtrl" id="thumbList">
<li ng-repeat="photo in photoData" ng-if="album.userId == photo.albumId">
<img id="view{{$index}}" ng-click="zoom($index)" ng-src={{photo.thumbnailUrl}}>
<p ng-click="delete($index)">x</p>
</li>
</ul>
</div>
</div>
</div>
任何帮助都太棒了!:) 从photoData中删除已删除的项目:
$scope.photoData.splice(index, 1);
事情是这样的:
var app = angular.module('myApp', []);
app.controller('AlbumCtrl', function ($scope, $http) {
$http.get("https://jsonplaceholder.typicode.com/albums").then(function(response) {
$scope.albumData = response.data;
console.log($scope.albumData);
});
});
app.controller('PhotoCtrl', function($scope, $http) {
$http.get("https://jsonplaceholder.typicode.com/photos").then(function(response) {
$scope.photoData = response.data;
});
$scope.delete = function(index) {
//Remove item from photoData
$scope.photoData.splice(index, 1);
var del = "/" + (index + 1);
$http.delete("https://jsonplaceholder.typicode.com/photos" + del).then(function(response) {
console.log($scope.albumData);
});
};
太棒了,这真是奇迹!我想我必须编辑photoData对象,并包括http.delete请求。此外,在通过API删除项目后编辑photoData对象可能更有意义:
$http.delete(“https://jsonplaceholder.typicode.com/photos“+del).then(函数(响应){$scope.photoData.splice(索引,1);console.log($scope.albumData);})代码>你完全正确!我将splice方法移到了中。然后()也感谢您帮助我设置格式!:)
var app = angular.module('myApp', []);
app.controller('AlbumCtrl', function ($scope, $http) {
$http.get("https://jsonplaceholder.typicode.com/albums").then(function(response) {
$scope.albumData = response.data;
console.log($scope.albumData);
});
});
app.controller('PhotoCtrl', function($scope, $http) {
$http.get("https://jsonplaceholder.typicode.com/photos").then(function(response) {
$scope.photoData = response.data;
});
$scope.delete = function(index) {
//Remove item from photoData
$scope.photoData.splice(index, 1);
var del = "/" + (index + 1);
$http.delete("https://jsonplaceholder.typicode.com/photos" + del).then(function(response) {
console.log($scope.albumData);
});
};