Javascript 角度,将对象推送到父阵列(模式)
我有一个模态控制器,就像这样Javascript 角度,将对象推送到父阵列(模式),javascript,angularjs,bootstrap-modal,Javascript,Angularjs,Bootstrap Modal,我有一个模态控制器,就像这样 angular.module('myApp').controller('ModalDemoCtrl', function ($scope, $modal, $log) { $scope.arrayList = []; $scope.newItem = function () { var modalInstance = $modal.open({ templateUrl: 'newItem.html', contro
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());
});
};
当我第一次打开一个模式窗口时,它将创建一个“newItem”,然后在该窗口内,我打开另一个模式来创建“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.logeditable
虽然此arrayList将位于另一个对象(第一个模式的可编辑对象)中,但它现在已经足够好了。您可以创建plunker plz吗?我可以尝试,以前从未做过。如果您有任何问题,请告诉我plunker有何进展??所发生的事情是,每个模态创建一个不同的$scope,因此您的$scope.arrayList在每个模态中都不相同。为了让它工作,你可以尝试将arrayList放在$rootScope.arrayList上,这是一个肮脏而快速的修复,有更好的解决方案。啊,我是sry,我在工作,我只有午饭时间发布我的问题,我回家后会和plunker一起工作。我尝试了rootscope解决方案,它说它是未定义的,也许它毕竟不是一个范围问题