我需要了解「如何訪問一個模塊中定義的$ rootScope值的值到其他模塊?」
下面是我的代碼:
的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";
}
]);
現場演示:http://plnkr.co/edit/X0aamCi9ULcaB63VpVs6?p=preview
經過下面的例子,但沒有奏效:
可能的[AngularJS Modules/Scope Sharing]重複(http://stackoverflow.com/questions/20751172/angularjs-modules-scope-sharing) –
@Sergiu Paraschiv:我不認爲這是重複的。我想在我的例子中訪問$ rootScope的值而不是$ scope。 –
同樣的事情。任何地方的$ scope都是$ rootScope樹的一部分。閱讀https://docs.angularjs.org/api/ng/service/$rootScope和https://docs.angularjs.org/guide/scope –