首页 > Web开发 > 详细

angularJS使用rootscope创建父域和子模态框通用的属性与函数

时间:2017-03-07 13:20:18      阅读:184      评论:0      收藏:0      [点我收藏+]

1. 在声明创建controller指明引用$rootscope

reviewInterfaceDo.controller(‘reviewInterfaceDo‘, function($scope, $rootScope, $http, $uibModal, $uibModalInstance, toastr,parameterRecord,user)

 

 2. 在controller中声明创建rootscope型变量commonScope ,并配置此scope内的函数和属性

//define root scope for modal use
	var commonScope = $rootScope.$new();
	commonScope.rootSaveScore  = function (userScoreList) {
		if (userScoreList != null && userScoreList.length > 0) {
			var fd = new FormData();
			var userScoreRecords = angular.toJson(userScoreList);
			fd.append(‘userScoreRecords‘, userScoreRecords);
			$http.post(‘/reviewProcess/save‘, fd, {
				transformRequest: angular.identity,
				headers: {
					‘Content-Type‘: undefined
				}
			})
			.success(function (data){
				toastr.success("submit "+data+" score record success");
			})
			.error(function (data) {
				toastr.error("submit failed");
			});
		}
		
	};

3.  在打开模态框的方法中传入commonScope 

$scope.openModal= function() {
		$scope.getReviewedScore();
		$uibModal.open({
			templateUrl: ‘save.html‘,
			controller: ‘saveController‘,
			scope: commonScope,
			resolve: {
					parentModalInstance: function() {
						return $uibModalInstance;
					},
					userScoreList : function () {
						return $scope.userScoreList;
					}
				}
		});
	}

 4. 在子模态框的controller中接收scope

saveController.controller(‘saveController‘, function($scope, $http, $uibModalInstance, parentModalInstance, toastr,userScoreList)

5. 最后就可以在子模态框的controller中调用commonScope 中的方法和属性啦

$scope.save= function() {
		//save reviewed score
		$scope.rootSaveScore($scope.userScoreList);
		//close modal
		$uibModalInstance.dismiss(‘cancel‘);
		parentModalInstance.close();
	};

  

 

angularJS使用rootscope创建父域和子模态框通用的属性与函数

原文:http://www.cnblogs.com/nelson-hu/p/6514075.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!