简体   繁体   English

AngularJS-ng-show无法正常工作

[英]AngularJS - ng-show not working as intended

I'm trying to hide to <a> tags, and then show them when I'm logged in, using the isLoggedIn() function, but the <a> tag is showing no matter what. 我试图隐藏到<a>标记,然后使用isLoggedIn()函数在登录时显示它们,但是<a>标记无论如何都显示。

HTML (pool-details.html) HTML(pool-details.html)

<header class="hero-unit" id="banner" ng-include="'components/header/header.html'"></header>

<div ng-include="'components/navbar/navbar.html'"></div>

<div class="container"> <hr>
    <div class="col-md-3 left-col-3">  
        <div ng-include="'../components/sidebar/sidebar.html'"></div>
    </div>

    <div class="col-md-5">
        <div class="details-pic">
            <img class="pool-details-pic" ng-src="{{ pool.picture }}"/>
        </div>
    </div>

    <div class="col-md-4">
        <h3 class="details-name">{{ pool.name }}</h3>
        <p class="details-info">Varenummer: {{ pool.number }}</p>
        <p class="details-info">Lager status: {{ pool.inStock }}</p>
        <p class="details-info">Dimensioner: {{ pool.width }}m x {{ pool.length }}m x {{ pool.height }}m</p>
        <hr>
        <p class="details-info">{{ pool.info }}</p>
    </div>
    <div class="col-md-12">
        <a ng-show="'isLoggedIn()'" ng-click="deleteProduct(pool)" class="delete-product">Slet</a>
        <a ng-show="'isLoggedIn()'" ng-click="editProduct(pool)" class="edit-product">Opdater</a>
    </div>

</div>

<footer class="footer" ng-include="'components/footer/footer.html'"></footer>

CONTROLLER (pool-details.controller.js) 控制器(pool-details.controller.js)

'use strict';

angular.module('welldanaJavascriptApp')
    .controller('poolDetailsCtrl', function ($scope, $stateParams, $state, poolService, Auth) {

        $scope.pool = {};
        poolService.find($stateParams.poolId, function(pool) {
             $scope.pool = pool;
        });

        $scope.deleteProduct = function(product){
            poolService.delete(product._id, function(){
                $state.go('pools');   
            });
        };
});

I can't wrap my head around why the <a> tag is still showing, though I'm not logged in. I'm using the Passport plugin for an Angular fullstack application. 尽管我尚未登录,但我仍无法确定为什么<a>标记仍会显示。我正在为Angular Fullstack应用程序使用Passport插件。

ng-show="'isLoggedIn()'"

它会计算'isLoggedIn()'字符串,并且始终为true,因此您应将其重构为ng-show="isLoggedIn()"

Okay I managed to sort out my problem. 好吧,我设法解决了我的问题。 It was a simple rookie mistake from my hand, my apologies. 这是我的道歉,这是一个简单的菜鸟错误。

Solution

I was missing the $scope.isLoggedIn() 我错过了$scope.isLoggedIn()

I simply addes this to the controller: 我只是将其添加到控制器中:

$scope.isLoggedIn = function(){
    return Auth.isLoggedIn();
}

And just returning the Auth.isLoggedIn() to actually add this value to my scope, thereby being able to check it in the HTML. 只需返回Auth.isLoggedIn()即可将此值实际添加到我的范围中,从而可以在HTML中对其进行检查。

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

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