Javascript Angularjs如何在REST操作后更新视图?
我正在使用Angular,删除一行后需要刷新视图。 当前代码工作正常,但表根本没有更新 知道如何更改我的代码吗 "严格使用",; app.controller('locationsController',['$scope','locationsService',function($scope,locationsService){ }])) HTML: HTML: 试试这个: view.htmlJavascript Angularjs如何在REST操作后更新视图?,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我正在使用Angular,删除一行后需要刷新视图。 当前代码工作正常,但表根本没有更新 知道如何更改我的代码吗 "严格使用",; app.controller('locationsController',['$scope','locationsService',function($scope,locationsService){ }])) HTML: HTML: 试试这个: view.html <div> <div> <table>
<div>
<div>
<table>
<thead>
<tr>
<th>Action</th>
<th>LocationId</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="location in locations">
<td>
<a href="#/login">Edit</a>
<button ng-click="deleteLocation(location.locationId,$index);">Delete</button>
</td>
<td>
{{ location.locationId }}
</td>
<td>
{{ location.name }}
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>LocationId</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>
{{ location.locationId }}
</td>
<td>
<input type="text" name="{{ location.name }}">
</td>
</tr>
</table>
</div>
试试这个:
view.html
<div>
<div>
<table>
<thead>
<tr>
<th>Action</th>
<th>LocationId</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="location in locations">
<td>
<a href="#/login">Edit</a>
<button ng-click="deleteLocation(location.locationId,$index);">Delete</button>
</td>
<td>
{{ location.locationId }}
</td>
<td>
{{ location.name }}
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>LocationId</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>
{{ location.locationId }}
</td>
<td>
<input type="text" name="{{ location.name }}">
</td>
</tr>
</table>
</div>
是在删除后以某种方式修改
$scope.locations
还是在删除后检索新列表?我已经发布了locationsService的代码。我相信目前删除后$scope.locations不会被触动。删除后,请从$scope.locations
中删除删除的位置,因为您使用的是ng repeat,一旦从数组中删除,该指令将自动从视图中删除它。您能否提供一个代码示例作为答案?感谢各位。删除数组项的建议?是在删除后以某种方式修改$scope.locations
还是在删除后检索新列表?我已经发布了locationsService的代码。我相信目前删除后$scope.locations不会被触动。删除后,请从$scope.locations
中删除删除的位置,因为您使用的是ng repeat,一旦从数组中删除,该指令将自动从视图中删除它。您能否提供一个代码示例作为答案?感谢各位。删除数组项的建议?确保使用$scope.ArrayNameNote.splice(索引,1)。确保使用$scope.ArrayNameNote.splice(索引,1)。
'use strict';
app.factory('locationsService', ['$http', function ($http) {
var serviceBase = 'http://localhost:4014/';
var locationsServiceFactory = {};
var _getLocations = function () {
return $http.get(serviceBase + 'api/locations').then(function (results) {
return results;
});
};
var _deleteLocation = function (locationId) {
return $http.delete(serviceBase + 'api/locations/' + locationId).then(function (results) {
return results;
});
};
locationsServiceFactory.getLocations = _getLocations;
locationsServiceFactory.deleteLocation = _deleteLocation;
return locationsServiceFactory;
}]);
<button ng-click="deleteLocation(location.locationId, $index);">Delete</button>
$scope.deleteLocation = function (locationId, index) {
locationsService.deleteLocation(locationId).then(function(){
$scope.locations.splice(index, 1)
}); // UPDATE THE VIEW
};
<div>
<div>
<table>
<thead>
<tr>
<th>Action</th>
<th>LocationId</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="location in locations">
<td>
<a href="#/login">Edit</a>
<button ng-click="deleteLocation(location.locationId,$index);">Delete</button>
</td>
<td>
{{ location.locationId }}
</td>
<td>
{{ location.name }}
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>LocationId</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>
{{ location.locationId }}
</td>
<td>
<input type="text" name="{{ location.name }}">
</td>
</tr>
</table>
</div>
$scope.deleteLocation = function (locationId,index) {
return locationsService.deleteLocation(locationId).then(function()
{
$scope.locations.splice(index,1);
}); // UPDATE THE VIEW
};