[英]How to call a sibling scope in angular js using rootscope
这是我的app.js
var app = angular.module("app",[]);
app.controller("myFirstController",['$scope',function($scope){
$scope.test = "myfirstName";
$scope.test1 = "myfirstName";
}])
app.controller("mySecondController",['$scope','$rootScope',function($scope,$rootScope){
$scope.test = "myLastName";
//Here how to call my test,test1 scope using $rootScope ??
}])
我能做到这一点使用$ rootScope?我不想使用任何emit
和broadcast
或this
如果您在rootscope中存储任何值,例如:
$rootScope.test =" value ";
您可以像这样从rootScope检索值
var value = $rootScope.test;
但是,它不能用于数据维护和安全性
因为如果刷新浏览器,则将清除所有作用域值。
所以我建议你用3种不同的方式
我不想使用任何发射和广播或
你可以像下面那样做,但是在某些情况下可以使用不同的名字来避免错误。
<!DOCTYPE html> <html ng-app="plunker"> <head> <meta charset="utf-8" /> <title>AngularJS Plunker</title> <script>document.write('<base href="' + document.location + '" />');</script> <link rel="stylesheet" href="style.css" /> <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script> <script src="app.js"></script> </head> <body > <div ng-controller="myFirstController"> </div> <div ng-controller="mySecondController"> </div> </body> <script> var app = angular.module('plunker', []); app.controller("myFirstController",['$scope','$rootScope',function($scope,$rootScope){ $rootScope.test = "myfirstName"; }]) app.controller("mySecondController",['$scope','$rootScope',function($scope,$rootScope){ $scope.test = "myLastName"; console.log($rootScope.test); //Here how to call my myFirstName scope using $rootScope ?? }]) </script> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.