Angularjs如何在REST操作后更新视图?

Gib*_*boK 1 javascript angularjs angularjs-scope

我正在使用Angular,我需要在删除一行后刷新视图.当前代码正常工作,但表根本不更新.

知道如何更改我的代码吗?

'使用严格'; app.controller('locationsController',['$ scope','locationsService',function($ scope,locationsService){

$scope.locations = [];

locationsService.getLocations().then(function (results) {

    $scope.locations = results.data;

}, function (error) {
    //alert(error.data.message);
});

$scope.deleteLocation = function (locationId) {
    locationsService.deleteLocation(locationId); // UPDATE THE VIEW
};
Run Code Online (Sandbox Code Playgroud)

}]);

<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);">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>
</div>
Run Code Online (Sandbox Code Playgroud)
'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;

}]);
Run Code Online (Sandbox Code Playgroud)

syl*_*ter 5

HTML:

 <button ng-click="deleteLocation(location.locationId, $index);">Delete</button>
Run Code Online (Sandbox Code Playgroud)

JS

$scope.deleteLocation = function (locationId, index) {
    locationsService.deleteLocation(locationId).then(function(){
             $scope.locations.splice(index, 1)

}); // UPDATE THE VIEW
};
Run Code Online (Sandbox Code Playgroud)