javascript - 从索引页 Angular 检查位置

标签 javascript angularjs angularjs-directive angularjs-scope

如果我在某些位置,我想隐藏导航栏。 A 有索引页:

<navbar ng-show="showNavbar"></navbar>
<div ng-view></div>

用js:

scope.showNavbar=(window.location.hash == '#/login');

我尝试在 mainCtrl 上放置一个位置检查器,但它并没有只检查一次位置。 之后,我将其放入 goClick 指令

.directive( 'goClick', function ( $location) {
        return function ( scope, element, attrs ) {
            var path;

            attrs.$observe( 'goClick', function (val) {
                path = val;
            });

            scope.showNavbar=(window.location.hash == '#/login');


            element.bind( 'click', function () {
                scope.$apply( function () {
                    $location.path( path );
                });
            });
        };
    });

它检查了它,但无法传递 html 页面和 Ctrl 的值。

最佳答案

改变

<navbar ng-show="showNavbar"></navbar>

<navbar ng-show="showNavbar()"></navbar>

和 Controller

scope.showNavbar = function(){
   if($location.path() =='/login'){
    return true; 
   }else{
     return false; 
    }
} 

关于javascript - 从索引页 Angular 检查位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42315954/

相关文章:

javascript - 当输入获得焦点时将类添加到输入,当不获得焦点时将其删除

javascript - 使用父列作为子行的 DataTables 行详细信息

javascript - 为什么这个 jQuery 代码片段在 IE8 中不能像在 Firefox 或 Chrome 中那样工作(包括现场演示)?

AngularJs - 'merge' 函数的问题

javascript - 启动 Angular Controller 的更好方法

javascript - 来自服务器的行未显示在 Ag-grid 中?

javascript - Controller 、指令和服务之间的 Angularjs 数据绑定(bind)

javascript - 通过 Javascript 调用 SQLite 时 Firefox 出错

javascript - 如何在同一 ui-select 字段上同时应用过滤器 :$select. 搜索和 limitTo :$select. infiniteCurrentLimit?

javascript - 将内容添加到列表时保持滚动位置 (AngularJS)