Angularjs将run变量连接到controller

时间:2015-09-01 13:26:46

标签: angularjs

.run(function ($rootScope, $location, Data) {
        $rootScope.$on("$routeChangeStart", function (event, next, current) {
            $rootScope.authenticated = false;
            Data.get('session').then(function (results) {
                if (results.uid) {
                    $rootScope.authenticated = true;
                    $rootScope.uid = results.uid;
                    $rootScope.name = results.name;
                    $rootScope.email = results.email;
                } else {
                    var nextUrl = next.$$route.originalPath;
                    if (nextUrl == '/signup' || nextUrl == '/login') {

                    } else {
                        $location.path("/login");
                    }
                }
            });
        });
    });

现在我想用$ rootscope.authenticated做的是隐藏基于真或假的菜单

<div ng-hide="" class="collapse navbar-collapse navbar-menubuilder">
        <ul class="nav navbar-nav navbar-left">
            <li><a href="#/dashboard">Home</a>
            </li>
            <li><a href="#/meldingen">Meldingen</a>
            </li>
            <li><a href="#/about-us">About Us</a>
            </li>
            <li><a href="/contact">Contact Us</a>
            </li>
        </ul>
    </div>

这是现在我尝试使用ng-hide =“authenticated”的菜单,但它似乎没有成功,我不知道如何获得经过身份验证的值到控制器

希望你能帮帮我

1 个答案:

答案 0 :(得分:0)

$rootScope注入控制器并公开authenticated,如下所示:$scope.authenticated = $rootScope.authenticated,确保控制器在该临时文件中可用。我过去通过在AppCtrl

整个网站上宣布<html ng-controller="AppCtrl">来解决这个问题