【问题标题】:Checking deletion and update ui angular ngResource module检查删除和更新 ui angular ngResource 模块
【发布时间】:2015-10-10 08:07:45
【问题描述】:

如何检查以下删除功能中实际发生的情况?每次我删除它都会显示“成功”,但 UI 不会更新。

HTML

<md-content >
  <div id="main" class="well">
    <table cellpadding="20" class="table table-bordered table-striped">
      <tr>
        <th ng-repeat="(head, value) in models[0]">
          <span>{{head}}</span>
        </th>
      </tr>
      <tr ng-repeat="row in models">
        <td ng-repeat="(name, value) in row" ng-scope>
          <span ng-click="" ng-bind="row[name]"></span>
        </td>
        <td >
          <a target="_self" href="#" ng-click="downlaodId(row)">Downlaod</a>
        </td>
        <td >
          <a target="_self" href="#" ng-click="deleteId(row)" confirmation-needed="Really Delete?">Delete</a>
        </td>  
      </tr>
    </table>
  </div>
</md-content>

控制器

$scope.deleteId = function (idPassed) {
  fileLoadService.delete({ 'id': idPassed.id }, function(successResult) {
    alert('Deleted');
  }, function (errorResult) {
    // do something on error
    if (errorResult.status === 404) {
      alert('Ooops');
    }
  });
};

点击删除后我的用户界面如下所示 文件加载服务

app.factory('fileLoadService', ['$resource',
  function ($resource) {
    return $resource(
      "http://jsonplaceholder.typicode.com/todos/:id",
      { id: "@id" },
      { query: { 'method': 'GET', isArray: true }
    });
 }]);

【问题讨论】:

  • 你应该在删除时更新你的model

标签: javascript angularjs rest api ngresource


【解决方案1】:

从代码中可以看出:

$scope.deleteId = function (idPassed) {

    fileLoadService.delete({ 'id': idPassed.id },function(successResult) {
        alert('Deleted');
    }, function (errorResult) {

您没有对当前模型做任何事情,只是在您点击删除按钮时发送警报Deleted。如果你想让它做其他事情......你应该把这个功能放在代码中。

例如:

$scope.deleteId = function (idPassed) {

        fileLoadService.delete({ 'id': idPassed.id },function(successResult) {
            var index = $scope.models.indexOf(idPassed);
            $scope.models.splice(index, 1);
            alert('Deleted');
        }, function (errorResult) {

【讨论】:

  • 你是什么意思?这是由 UI 中的按钮调用的方法 (deleteId(id))。 fileLoad 服务返回资源。我想念她什么
  • 您的模型永远不会更新。如果您希望更改反映在当前屏幕上,则需要更新模型。请检查我上面发布的代码。
  • 谢谢,它在 ui 上工作,但我不确定 api 是否在 api 上工作删除,因为当我重新加载页面时它刚刚出现。 jsonplaceholder api 是否会持续更改一段时间
  • 如果是这样,那是您的 API 或 fileLoadService 的问题。
  • 这是可以加载 api 的服务 app.factory('fileLoadService', ['$resource', function ($resource) { return $resource( "jsonplaceholder.typicode.com/todos/:id", { id : "@id" }, { "query": { 'method': 'GET', isArray: true } }); }]);
猜你喜欢
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多