angular,将对象推送到父数组(模态)

angular, push object to parent array (modal)

我有一个模态控制器,就像这样

angular.module('myApp').controller('ModalDemoCtrl', function ($scope, $modal, $log) {

  $scope.arrayList = [];

  $scope.newItem = function () {
    var modalInstance = $modal.open({
        templateUrl: 'newItem.html',
        controller: 'newItemCtrl',
        windowClass: 'app-modal-window',
        backdrop: 'static',
        resolve: {
        }
    });
    modalInstance.result.then(function (editable) {

        console.log($scope.arrayList);

    }, function () {
        $log.info('Modal dismissed at: ' + new Date());
    });
};

  $scope.newArrayItem = function () {
    var modalInstance = $modal.open({
        templateUrl: 'newArrayItem.html',
        controller: 'newArrayCtrl',
        windowClass: 'app-modal-window',
        backdrop: 'static',
        resolve: {
        }
    });
    modalInstance.result.then(function (editable) {

        $scope.arrayList.push(editable);

    }, function () {
        $log.info('Modal dismissed at: ' + new Date());
    });
};

当我第一次打开模态 window 时,它会创建一个 'newItem',然后在其中 window 我打开另一个模态来创建 'ArrayItems',当一个 arrayItem 是创建(当该模式关闭时)我想将该项目推送到我的 $scope.arrayList,并重复,当创建所有 arrayItems 我也关闭 'newItem' 模式,这就是我想要到达的地方我的 $scope.arrayList,但是当我尝试记录它时,它是空的。

所以我想我需要将对象推送到父范围,我该怎么做?

我找到了一个解决方案,不知道它是否是最优的,但就是这样。

我无法在原处启动数组,我必须在第一个模态的控制器内部启动

angular.module('myApp').controller('newItemCtrl', function ($scope, $modalInstance) {

$scope.arrayList = [];

  $scope.editable = {
    arrayList: $scope.arrayList
  };

  $scope.ok = function () {
    $modalInstance.close($scope.editable);
  };

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };
});

如您所见,我还必须将它包含在我的可编辑对象中,以便在我们关闭模式时传递它。

然后在关闭第二个模式时,我在其中创建 arrayList 项目,我只是像以前一样推送它 $scope.arrayList.push($scope.editable);

最后,当我关闭第一个模式时,我可以记录我的 arrayList console.log(editable);

尽管此 arrayList 将位于另一个对象(第一个模态的可编辑对象)内,但现在已经足够了。