我无法弄清楚如何从子作用域(ModalCtrl)更新父作用域(DirCrl)中的$scope.dir.视图是一个简单的模态形式,只有一个文本输入.提交时,文本输入绑定到子范围中的mkdir.name.子控制器对数据库进行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,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,config) { $scope.mkdir.data = data; }) .error(function (data,config) { }); $modalInstance.dismiss('cancel'); } $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; };
更正:
原文链接:https://www.f2er.com/angularjs/141658.html在孩子做类似的事情:
$scope.$emit('whatevereventnameyouwant',data);
在父母听取该事件:
$rootScope.$on('whatevereventnameyouwant',function(event,data) { console.log(data); });