Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AngularJS scope的一个特性:刷新子scope的时候,也会刷新父scope;反之亦然大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

最近在开发项目的时候,发现了AngularJS一个很好玩的特性。我们知道AngularJS的scope存在父子关系,当我们通过scope.$apply()手动刷新子作用域的时候,父作用域也会被刷新;反过来也是如此。

<!doctype html>
<html lang="en" ng-app>
	<head>
	   <Meta charset="utf-8">
	   <title>ng-model</title>
	   <script src="angular-1.2.25.js"></script>
	   <script>
			var outerScope = null;
			var innerScope = null;
			function outerController($scopE)
			{
				outerScope = $scope;
				outerScope.out = "out";		
			}
			
			function innerController($scopE)
			{
				innerScope = $scope;
				innerScope.in = "in";		
			}
			
			function outerFunc()
			{
				innerScope.in = "parent";
				outerScope.out = "parent";
				// 刷新父scope
				outerScope.$apply();
			}
			
			function innerFunc()
			{	
				innerScope.in = "child";
				outerScope.out = "child";
				// 刷新子scope
				innerScope.$apply();
			}
	   </script> 
	</head>
	
	<body ng-controller="outerController">
			{{out}}
		<button onclick="outerFunc();">outerFunc</button>
		
		<div ng-controller="innerController">
			{{in}}
			<button onclick="innerFunc();">innerFunc</button>
		</div>
	</body>
	
</html>

大佬总结

以上是大佬教程为你收集整理的AngularJS scope的一个特性:刷新子scope的时候,也会刷新父scope;反之亦然全部内容,希望文章能够帮你解决AngularJS scope的一个特性:刷新子scope的时候,也会刷新父scope;反之亦然所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。