6

在我的角度应用程序中,我有一个控制器,如下所示:

function TemplateListControl($scope, TemplateService){
    $scope.templates = TemplateService.get(); // Get objects from resource

    // Delete Method
    $scope.deleteTemplate = function(id){
        $scope.templates.$delete({id: id});
    }
}

在视图中,我有一个绑定到templates模型的表。如下:

<table ng-model="templates">
    <thead>
        <tr>
            <th style="width:40%">Title</th>
            <th style="width:40%">controls</th>
        </tr>
    <thead>
    <tbody>
        <tr ng-repeat="template in templates">
            <td>
                <!-- Link to template details page -->
                <a href="#/template/[[template.id]]">
                    [[template.title]]
                </a>
            </td>
            <td>
                <!-- Link to template details page -->
                <a class="btn btn-primary btn-small"
                   href="#/template/[[template.id]]">Edit
                </a>
                <!-- Link to delete this template -->
                <a class="btn btn-primary btn-small"
                   ng-click="deleteTemplate(template.id)">Delete
                </a>
            </td>
        </tr>
    </tbody>
</table>

现在,当我单击上述模板中的删除链接时,它会调用该deleteTemplate方法并成功DELETE调用 REST api。$scope.templates = TemplateService.get();但是视图在刷新并再次启动之前不会更新。我究竟做错了什么?

4

3 回答 3

3

我更喜欢使用承诺而不是回调。Promise 是处理异步进程的新方法。您可以在结果从服务器返回后立即使用 apromise 检查结果。

//Controller
myApp.controller('MyController',
    function MyController($scope, $log, myDataService) {

 $scope.entities = myDataService.getAll();
 $scope.removeEntity = function (entity) {       
        var promise = myDataService.deleteEntity(entity.id);
        promise.then(
            // success
            function (response) {
                $log.info(response);
                if (response.status == true) {
                    $scope.entities.pop(entity);
                }
            },
            // fail
            function (response) {
                $log.info(response);
                // other logic goes here
            }
        );
    };
 });

 //DataService
 myApp.factory('myDataService', function ($log, $q, $resource) {

return {
    getAll: function () {
        var deferred = $q.defer();
        $resource('/api/EntityController').query(
            function (meetings) {
                deferred.resolve(meetings);
            },
            function (response) {
                deferred.reject(response);
            });

        return deferred.promise;
    },

    deleteEntity: function (entityId) {
        var deferred = $q.defer();
        $resource('/api/EntityController').delete({ id: entityId},
            function (response) {
                deferred.resolve(response);
            },
            function (response) {
                deferred.reject(response);
            });

        return deferred.promise;
    }
   };
});

//Web API Controller
public class MeetingController : BaseApiController
{
   // .... code omited 

   public OperationStatus Delete(int entityId)
    {
        return _repository.Delete(_repository.Single<MyEntity>(e => e.EntityID == entityId));
    }
}

注意:$log、$q、$resource 是内置在服务中的。希望能帮助到你 :)

于 2013-11-11T15:26:12.710 回答
1

您还必须更新客户端,因此修改您的源代码如下

 ng-click="deleteTemplate($index)">


$scope.delete = function ( idx ) {
  var templateid = $scope.templates[idx];

  API.Deletetemplate({ id: templateid.id }, function (success) {
    $scope.templates.splice(idx, 1);
  });
};
于 2013-03-15T11:10:36.700 回答
0

您可以将回调函数传递给 $resource.$delete

function TemplateListControl($scope, TemplateService){
  $scope.templates = TemplateService.get(); // Get objects from resource

  // Delete Method
  $scope.deleteTemplate = function(id){
    TemplateService.$delete({id: id}, function(data) {
      $scope.templates = data;
    });
  }
}

注意 如果您的 REST API 删除函数返回一个数组,您必须在 Angular $resource 中设置 isArray: true 以避免 AngularJS $resource 错误 - TypeError: Object # has no method 'push'

$resource(URL, {}, {
  delete: {method:'DELETE', isArray: true}
});
于 2013-08-11T12:37:41.550 回答