简体   繁体   English

ng-include和ng-view中的scope变量

[英]scope variable in ng-include and ng-view

I have a problem with my scope variables. 我的范围变量有问题。 Actually, I do have an index.html file where we can find a navbar added with a ng-include : 实际上,我确实有一个index.html文件,在这里我们可以找到添加了ng-include的导航栏:

<div class=col-xs-9 sidebar-offcanvas" id="sidebar" role="navigation ng-include="'views/common/navside.html'"></div>

And just after I have my ng-view : 在我有ng-view之后:

<div class="col-xs-12 content" ng-view=""></div>

Here is my problem : In my navbar, I show if I am logged or not, and when I log myself, the view changes with a $location.path, and I initialize a $scope.$parent variable with the login of the user. 这是我的问题:在导航栏中,显示是否登录,并且在登录自己时,视图会使用$ location.path更改,并使用用户登录名初始化$ scope。$ parent变量。 。 If I refresh the page, I get the $scope.$parent variable in the navbar, but if I don't, I get nothing and I still have my "Log in" choice 如果刷新页面,则会在导航栏中获得$ scope。$ parent变量,但如果没有,则什么也不会得到,并且仍然可以选择“登录”

My navside.html page search the right variable as I'm doing this : 我的navside.html页面在执行此操作时搜索正确的变量:

<div ng-if="$parent.login.length > 0"> ... </div>
<div ng-if="$parent.login.length == 0"> ... </div>

Is there any way to be able to have a scope variable which is common to the ng-include and ng-view? 有什么方法可以拥有ng-include和ng-view共有的作用域变量?

Thank you 谢谢

My answer is simple: use $rootScope.$broadcast('Login'); 我的答案很简单:使用$ rootScope。$ broadcast('Login'); when you login. 登录时。

And in the navbar Ctrl: 并在导航栏中Ctrl:

$rootScope.$on('Login', function () {
   // set the $scope.$parent variable
})

Hope this helps. 希望这可以帮助。

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

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