如何将一个模块中定义的 $rootScope 值访问到另一个模块中 [英] How to access $rootScope value defined in one module into another module
问题描述
我需要了解如何将一个模块中定义的 $rootScope 值访问到另一个模块中?"
下面是我的代码:
Index.html
<html ng-app="myapp"><头><script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"><link rel="stylesheet" href="style.css"><script src="script.js"></script><script src="test.js"></script>头部><div ng-controller="serviceDIController">服务价值是:<b>{{sdiParam}} </b><br/>出厂值是:<b>{{fdiParam}} </b><br/>根范围值是:<b>{{rootParam}} </b>
</html>
script.js
var app = angular.module("myapp", ['testModule']);app.controller('serviceDIController',['$scope','$rootScope','testService','testFactory',function($scope, $rootScope,testService, testFactory){$scope.sdiParam = testService.param;$scope.fdiParam = testFactory.fparam;//$scope.rootParam = $rootScope.root;//如何访问控制器内当前模块中 test.js 中定义的$rootScope.root"值?}]);
test.js
var testapp = angular.module("testModule", []);testapp.service('testService', function() {this.param = "服务参数 1 DI";});testapp.factory('testFactory', function() {var 事实 = {};fact.fparam = "Fact Param1 DI";返回事实;});testapp.controller('testCtrl', ['$scope',函数($rootScope){$rootScope.root = "根范围参数1";}]);
现场演示:http://plnkr.co/edit/X0aamCi9ULcaB63VpVs6?p=preview
检查了下面的例子,但没有奏效:
在 testCtrl
中显式注入 '$scope'
,而不是 '$rootScope'
code>,因此会为该控制器创建一个新的作用域,并将其作为第一个参数传递,而不管该参数使用的名称是什么.
不正确:
testapp.controller('testCtrl', ['$scope',函数($rootScope){$rootScope.root = "根范围参数1";}]);
正确:
testapp.controller('testCtrl', ['$rootScope',函数($rootScope){$rootScope.root = "根范围参数1";}]);
I need to understand "How can I access the value of $rootScope value defined in one module into other module?"
Below is my code :
Index.html
<!DOCTYPE html>
<html ng-app="myapp">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"> </script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<script src="test.js"></script>
</head>
<div ng-controller="serviceDIController">
Service Values is : <b> {{sdiParam}} </b> <br/>
Factory Values is : <b> {{fdiParam}} </b> <br/>
Root Scope Values is : <b> {{rootParam}} </b>
</div>
</html>
script.js
var app = angular.module("myapp", ['testModule']);
app.controller('serviceDIController',['$scope','$rootScope',
'testService','testFactory',function($scope, $rootScope,testService, testFactory)
{
$scope.sdiParam = testService.param;
$scope.fdiParam = testFactory.fparam;
// $scope.rootParam = $rootScope.root; // How to access "$rootScope.root" value defined in test.js in current module inside a controller?
}
]);
test.js
var testapp = angular.module("testModule", []);
testapp.service('testService', function() {
this.param = "Service Param1 DI";
});
testapp.factory('testFactory', function() {
var fact = {};
fact.fparam = "Fact Param1 DI";
return fact;
});
testapp.controller('testCtrl', ['$scope',
function($rootScope) {
$rootScope.root = "Root Scope Param1";
}
]);
Live demo : http://plnkr.co/edit/X0aamCi9ULcaB63VpVs6?p=preview
Checked below example but did not work:
Explicitly inject '$scope'
, not '$rootScope'
in testCtrl
, so a new scope is created just for that controller and passed as the first argument regardless of the name used for that argument.
Incorrect:
testapp.controller('testCtrl', ['$scope',
function($rootScope) {
$rootScope.root = "Root Scope Param1";
}
]);
Correct:
testapp.controller('testCtrl', ['$rootScope',
function($rootScope) {
$rootScope.root = "Root Scope Param1";
}
]);
这篇关于如何将一个模块中定义的 $rootScope 值访问到另一个模块中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!