gpt4 book ai didi

模态表单提交后,Angularjs在父范围内更新数据

转载 作者:行者123 更新时间:2023-12-04 10:04:12 24 4
gpt4 key购买 nike

我无法弄清楚如何从子范围 (ModalCtrl) 更新父范围 (DirCrl) 中的 $scope.dir。该 View 是具有单个文本输入的简单模式表单。提交时,文本输入绑定(bind)到子范围内的 mkdir.name。子 Controller 对数据库进行 REST 调用,并应使用响应数据更新父作用域中的 $scope.dir。任何指导将不胜感激。下面的代码片段

app.controller('DirCtrl', ['$scope', '$http', function ($scope, $http) {

$scope.dir = {};
$scope.mySelections = [];

$http({
method: 'GET',
url: '//localhost:9090/fx/v1/dir/52cdc7304c3525ac0c5cdd3a'
})
.success(function (data, status, headers, config) {
$scope.dir = data;
$scope.children = data.children;
})
.error(function (data, status, headers, config) {
});

}]);

var ModalCtrl = function ($scope, $modal, $log) {

$scope.mkdir = {
name: 'name',
data: {}
};

$scope.$parent.ben = 'ben01';

$scope.open = function () {

var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
backdrop: true,
windowClass: 'modal',
controller: ModalInstanceCtrl,
resolve: {
mkdir: function () {
return $scope.mkdir;
}
}
});

modalInstance.result.then(function () {
$scope.$parent.children = $scope.mkdir.data.children;
});
};
};

var ModalInstanceCtrl = function ($scope, $modalInstance, $http, $log, mkdir) {
$scope.mkdir = mkdir;
$scope.submit = function () {
$log.log('name of directory to create');
$log.log(mkdir.name);

$http({
method: 'GET',
url: '//localhost:9090/fx/v1/dir/52cdcce74c358cdfe2fa2c83'
})
.success(function (data, status, headers, config) {

$scope.mkdir.data = data;
})
.error(function (data, status, headers, config) {

});
$modalInstance.dismiss('cancel');
}


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

最佳答案

更正:

在 child 做这样的事情:

$scope.$emit('whatevereventnameyouwant', data);

在父级监听该事件:
$rootScope.$on('whatevereventnameyouwant', function(event, data) { console.log(data); });

关于模态表单提交后,Angularjs在父范围内更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21357115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com