ngShow在$父变量? [英] ngShow on $parent variable?

查看:152
本文介绍了ngShow在$父变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到的东西在跑 $ rootScope 来为应用程序设置范围内的值 userLoggedIn

I've got something running in the $rootScope to set an application-wide value for userLoggedIn:

mod.run(function($rootScope) {
    $rootScope.userLoggedIn = false;

    $rootScope.$on('loggedIn', function(event, args) {
        $rootScope.userLoggedIn = true;
    });
});

和当用户登录我的杠杆作用 $发出来提高该事件:

and when the user is logged in I leverage $emit to raise that event:

mod.controller('FacebookController', function($scope) {
    $scope.loggedIn = function() {
        $scope.$emit('loggedIn');
    };
});

和这是工作。

接下来,我已配置属性指令带来的 $ rootScope 眼帘:

Next I've configured an attribute directive to bring the $rootScope into view:

mod.directive('rootScope', function() {
    return {
        scope: { userLoggedIn: '@' },
        restrict: 'A'
    };
})

然而,当我使用它的元素/隐藏基于关闭它的价值,以显示它不工作:

However, when I use it on the elements to show/hide based off its value it doesn't work:

<div class="row" root-scope>
    <div class="panel callout radius" ng-show="!userLoggedIn">

<form name="listForm" root-scope
    novalidate ng-submit="listForm.$valid && model.save()"
    ng-show="userLoggedIn">

为什么不说值进入范围是什么?当我看到Chrome的角度范围变量不是在范围内。这是关于 $父虽然符合市场预期。

更新

所以,如果我用 $ root.userLoggedIn 它引用属性不如预期,但认为仍不能当值改变作出反应。例如,如果我重新加载该页面是开始,但Facebook完成登录它设置为真正之后;认为没有响应。

So, if I use $root.userLoggedIn it does reference the property as expected, however, the view still doesn't respond when the value is changed. For example, if I reload the page it's false initially, but after Facebook is done logging in it's set to true; the view doesn't respond.

推荐答案

答案是令人难以置信的简单,利用注释了。我其实并只需要视图中使用 $ root.userLoggedIn ,但我也需要利用 $适用

The answer was unbelievably simple, and leveraged a comment too. I did in fact just need the view to use $root.userLoggedIn, but I also needed to leverage the $apply:

mod.run(function($rootScope) {
    $rootScope.userLoggedIn = false;

    $rootScope.$on('loggedIn', function(event, args) {
        $rootScope.$apply(function() {
            $rootScope.userLoggedIn = true;
        });
    });
});

这篇关于ngShow在$父变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆