angular 各scope 通信

前端之家收集整理的这篇文章主要介绍了angular 各scope 通信前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一:iframe 中的 controller 访问 父页面中的controller


a页面

<!DOCTYPE html>
<html ng-app="a">
<head>
<Meta charset="UTF-8">
<title></title>
<script src="lib/angular/angular.min.js"></script>

</head>
<body>
<div id="view" ng-controller="dsViewCtr"></div>
<script>
var app = angular.module('datasheet',[]);
app.controller('dsViewCtr',function($scope) {
$scope.fullScreen=function(){
var scope=$window.parent.angular.element($window.frameElement).scope();//访问父层的controller
if(scope.fullClass=="fullClass"){
scope.fullClass=""

}else{
scope.fullClass="fullClass" ;

}

scope.$apply();
}
});
</script>
</body></html>



 
 
b页面 
 

<!DOCTYPE html>
<html ng-app="b">
<head>
<Meta charset="UTF-8">
<title></title>
<script src="lib/angular/angular.min.js"></script>


</head>
<body ng-app="datasheet">


<div id="datasheetSrc" ng-controller="datasheetSrcCtr">
<iframe id="datasheetIframe" src="http://192.168.1.222:8080/a.jsp" ng-class= "fullClass"></iframe>
</div>
<script>
var app = angular.module('datasheet',[]);

app.controller('datasheetSrcCtr',function($scope)

{ $scope.fullClass = "full";});
</script>
</body>

</html>

 

二:外部的Js访问angular里面的根域

controller.js:

$rootScope.isActive=""

外部js:

var $body = angular.element(document.body);
var $rootScope = $body.scope().$root;
$rootScope.$apply(function () {
$rootScope.isActive=2 //把根的isActive变量改为2
})



三:外部的Js访问angular里面的其他controller的域

controller.js:

$scope.isActive=""

外部js:

angular.element(document.querySelector('[ng-controller=mainController]')).scope(). isActive=3//把 mainController里面的isActive改为3; 原文链接:https://www.f2er.com/angularjs/148543.html

猜你在找的Angularjs相关文章