简体   繁体   English

在模板视图中访问 rootScope 变量不起作用

[英]Accessing rootScope variable in template view not working

I know this is possible and have seen many threads on how to do this but I can't figure out what I am doing wrong.我知道这是可能的,并且已经看到很多关于如何做到这一点的线程,但我无法弄清楚我做错了什么。 My $rootScope variable is not appearing in my template view.我的$rootScope变量没有出现在我的模板视图中。

.run(function($ionicPlatform,$state,$rootScope,$q,apiService) {
  $ionicPlatform.ready(function() {

    $rootScope.test = "HELLO ALL" ;

 ...
 ...
}),

.controller('EventCtrl', function($scope,$rootScope,$state,$stateParams) {

}),

And in my EventCtrl webview template:在我的 EventCtrl webview 模板中:

<div style="color:red;font-weight:bolder;">{{$root.test}}</div>

Yet, HELLO ALL never appears.然而, HELLO ALL从未出现。 I have tried simply {{test}} as suggested in some other posts but that too is not working.我已经按照其他一些帖子中的建议简单地尝试了{{test}} ,但这也不起作用。 What am I getting wrong here?我在这里做错了什么? No errors are being generated in console.控制台中没有生成错误。

I also tried the following, in my EventCtrl controller I set:我还尝试了以下操作,在我设置的 EventCtrl controller 中:

$scope.globalScope = $rootScope ;

And then in template accessed: {{globalScope.test}} - and it still didn't work.然后在访问的模板中: {{globalScope.test}} - 它仍然无法正常工作。

You can simply use {{ test }} , since $scope inherits from $rootScope您可以简单地使用{{ test }} ,因为$scope继承自$rootScope

Check below:检查以下:

 angular.module("myApp", []).run(function($rootScope) { $rootScope.test = "Hello All" }).controller("myCtrl", function($scope, $rootScope) {})
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <div style="color:red;font-weight:bolder;">{{test}}</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM