【问题标题】:AngularJs Table not updated after Create/Delete/update创建/删除/更新后 AngularJs 表未更新
【发布时间】:2017-01-02 00:54:03
【问题描述】:

如果我在我的数组中创建/更新/删除值,ng-table 不会更新数据。我需要为此做一个 window.location.reload() ,但这不是很“漂亮”。在 Angularjs 中不应该通过 2Way DataBinding 和 Automatic $apply Cycle 它自己完成吗?
我要审查的代码可能我忘记了什么:

'use strict';
(function() {
  class TranslationsComponent {
    constructor($http, $scope, $uibModal) {
        this.$http = $http;
        this.$scope = $scope;
        this.$uibModal = $uibModal;
        this.langV = [];
      }
  $onInit() {// getting my Datas
        this.$http.get('/api/dict_keys/all/' + 1 + '/' + 1)
          .then(response => {
            this.langV = response.data;
          });

    }
    // For Example Deleting something with a Modal
    // Delete Modal
    deleteKey(selectedKey) {
        this.$uibModal.open({
          scope: this.$scope,
          templateUrl: 'delete.html',
          controller: ['$scope', '$uibModalInstance', '$http', 'selectedKey',
            function($scope, $uibModalInstance, $http) {
              $scope.selectedKey = selectedKey;
              this.$http = $http;
              $scope.close = function() {
                $uibModalInstance.dismiss();
              };
              $scope.delete = () => {
                this.$http.delete('/api/dict_keys/' + selectedKey._id)
                  .then(() => {
                    //window.location.reload();
                    //what can i instead of realod do?
                    toastr.success('The Key is successfully Deleted');
                    $uibModalInstance.close(); 
                  });
              };
            }
          ],
          resolve: {
            selectedKey: () => selectedKey
          }
        });
      } 
  /* ----------------------------------------- */
  angular.module('euconDictionaryApp')
    .component('translations', {
      templateUrl: 'app/translations/translations.html',
      controller: TranslationsComponent
    });
})(); 

简而言之,在我的 .html 中,它是一个简单的 ng-repeat 显示所有内容:

    <tr dir-paginate="v in $ctrl.langV |itemsPerPage: 10">
              <td>
               {{v.Name}}
              </td>
  <td>
            <!-- Delete Key Button -->
            <button type="button" class="btn btn-default" ng-click="$ctrl.deleteKey(v)">
            </button>
          </td>

【问题讨论】:

    标签: javascript angularjs angularjs-scope angularjs-ng-repeat angular-ui-bootstrap


    【解决方案1】:

    看起来您需要在删除或更新后更新“this.langV”数组才能看到更新。您可以使用 javascript splice 方法从数组中删除一个项目。

    删除后可以使用

    this.langV.splice(this.langV.indexOf(v), 1)
    

    更新后你可以像这样更新项目

    this.langV[index] = updateItem
    

    【讨论】:

    • this.langV 在模态中未定义...使用 Ui-Bootstrap 模态
    • 您需要连接关闭事件以将新值返回给控制器,然后使用新值重新填充 langV 属性
    • deleteKey(selectedKey) { var _this = this; ....请存储这样的顶级对象,你可以访问 _this.langV
    • wow thx 解决方案与 _this 解决了它以及我认为现在遇到的许多其他问题。在他们的文档中从未见过。所以你的答案被接受了。
    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2011-11-11
    • 2018-02-01
    • 2015-01-23
    相关资源
    最近更新 更多