1

我有一个控制器:

angular.module('mean').controller('LocationController', [
  '$scope', '$location', '$rootScope', '$aside', '$routeParams', '$filter',
  'ngTableParams', 'LocationService', 'UserService', 'CompanyService',
  function ($scope, $location, $rootScope, $aside, $routeParams, $filter,
    ngTableParams, LocationService,  UserService, CompanyService) {
      $rootScope.menuItem = 'locations';
      $scope.contentTemplate = '/views/location/index.html';
      $scope.locations = [];
      $scope.current_location = null;
      $scope.newLocation = {};
      $scope.location_parent_id = $routeParams.location_parent_id;
      $scope.validation_errors = [];


      $scope.index = function() {
        CompanyService.initialized.then(function() {
          var company_id = CompanyService.getCompany()._id;
          LocationService.list(company_id, $routeParams.location_parent_id).then(function(response) {
            if(response.data.status === 'ok') {
              $scope.locations = response.data.locations;
              $scope.current_location = response.data.location || null;
            } else {
              alert('TBD');
            }
          });
        });

        $scope.tableParams = new ngTableParams({
          page: 1,
          count: 10,
        }, {
          total: $scope.locations.length,
          getData: function($defer, params) {
            var orderedData = params.sorting() ? $filter('orderBy')($scope.locations, params.orderBy()) : $scope.locations;
            orderedData = params.filter() ? $filter('filter')(orderedData, params.filter()) : orderedData;

            $scope.locations = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());

            params.total(orderedData.length);
            $defer.resolve($scope.locations);
          }
        });
      }

      $scope.addLocationModal = function() {
        $scope.location_types = ['warehouse', 'section', 'row', 'shelf', 'bin'];
        $aside({
          scope: $scope,
          template: '/views/location/addLocationAside.html',
          show: true
        });
      }

      $scope.createLocation = function() {
        $scope.newLocation.company_id = CompanyService.getCompany()._id;
        LocationService.create($scope.newLocation).then(function(response) {
          if(response.data.status === 'ok') {
            $scope.$hide();
            $scope.index();
          } else {
            $scope.validation_errors = response.data.error;
          }
        });
      }
  }
]);

在模态中,我有一个表单,提交时调用createLocation函数。如果位置创建成功,我希望关闭模式并运行索引并重新更新列表。但这似乎并没有发生。我认为这是一个$scope问题,但我不确定是什么。

4

1 回答 1

1

没有更多细节,真的很难说出问题所在。所以这里有一些猜测:

适用范围

如果LocationService不处理$scope.$apply,您可能需要在内部回调中调用它createLocation才能使更改生效。例子:

$scope.createLocation = function() {
    $scope.newLocation.company_id = CompanyService.getCompany()._id;
        LocationService.create($scope.newLocation).then(function(response) {
            $scope.$apply(function () {
                if (response.data.status === 'ok') {
                    $scope.$hide();
                    $scope.index();
                } else {
                    $scope.validation_errors = response.data.error;
                }
            });
        });
};

您可能还需要在回调中的$scope.index方法中执行此操作。LocationService.list

初始化只解决一次

承诺可能CompanyService.initialized只在加载应用程序时解决一次,因此您$scope.locations在关闭模式后实际上并没有更新。我将其更改为:

var isCountryServiceInitalized = false;

$scope.index = function() {
    function updateLocations() {
        var company_id = CompanyService.getCompany()._id;
        LocationService.list(company_id, $routeParams.location_parent_id).then(function(response) {
            if (response.data.status === 'ok') {
                $scope.$apply(function () { // you don't need this if LocationService already calls $scope.$apply
                    $scope.locations = response.data.locations;
                    $scope.current_location = response.data.location || null;
                });
            } else {
                alert('TBD');
            }
        });
    }

    if (!isCountryServiceInitalized) { 
        CompanyService.initialized.then(function () {
            isCountryServiceInitalized = true;
            updateLocations();
        });
    } else {
        updateLocations();
    }

    ...
};

ngTable 重新加载

看起来您正在使用 ng-table。您可能需要在更新后重新加载表$scope.locations。采用:

$scope.tableParams.reload();
于 2014-03-04T10:30:09.637 回答