为什么此资源在使用$ delete方法后没有更新视图

时间:2013-03-15 10:23:01

标签: angularjs

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

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方法,并且对REST api进行了成功的DELETE调用。但是,在刷新并再次启动$scope.templates = TemplateService.get();之前,视图不会更新。我做错了什么?

3 个答案:

答案 0 :(得分:3)

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

//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是内置于服务中的。希望它有所帮助:)

答案 1 :(得分: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);
  });
};

答案 2 :(得分: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 $资源中设置isArray:true以避免AngularJS $资源错误 - TypeError:对象#没有方法&#39; push&#39;

$resource(URL, {}, {
  delete: {method:'DELETE', isArray: true}
});