【问题标题】:Showing and hiding header nav in Mean.js app在 Mean.js 应用程序中显示和隐藏标题导航
【发布时间】:2015-02-19 03:38:40
【问题描述】:

我正在使用Yeoman Mean.js generator 建立一个博客。到目前为止,我真的很享受使用 Mean.js 工作,但是我对 Angular 开发还比较陌生,所以有些事情还不适合我。

我想在我的应用程序的所有页面上隐藏标题,并且仅在我登录时显示它。我将从标题中提取注册,并从一个位置登录以管理我的博客。

我尝试在 /app/views/layout.server.view.html 中的 <header> 元素上使用 ng-show="topbarActive"

<header ng-show="topbarActive" data-ng-include="'/modules/core/views/header.client.view.html'" class="navbar navbar-fixed-top navbar-default"></header>

然后我尝试在 /public/modules/core/controllers/home.client.controller.js 中将此变量显式设置为 false

$scope.topbarActive = false;

我在 /public/modules/users/controllers/authentication.client.controller.js 中将此值设置为 true,希望我可以手动 ping http://localhost:3000/#!/signup 并查看我的标题栏。

设置完成后,我在任何地方都看不到标题。看到我对“角度方式”是多么陌生,为了实现我正在寻找的行为,我缺少哪些步骤。我是否对 Mean.js 如何构建它的依赖关系感到困惑?

【问题讨论】:

    标签: angularjs meanjs


    【解决方案1】:

    AngularJS 中的一个关键概念是作用域。在这种特殊情况下,您的控制器 HomeController 被分配给标题 的第一个 div。这使得 HomeController 范围对它的父级(标头)不可用,实际上根本没有任何控制器。

    为使您的配置生效,请添加一个新的控制器,例如:

    /* /public/modules/core/controllers/body.client.controller.js */
    'use strict';
    
    angular.module('core').controller('BodyController', ['$scope', 'Authentication', 'Menus',
        function($scope, Authentication, Menus) {
            $scope.topbarActive = true;
        }]);
    

    然后将这个控制器添加到layout.server.view.html中的body中:

    <body ng-cloak ng-controller="BodyController">
    

    或者,您可以只为您的标签创建一个控制器,这取决于您从这里开始的位置。

    【讨论】:

    • 我知道这将是非常简单的事情。非常感谢您为我指明了正确的方向!
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2018-10-24
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多