简体   繁体   中英

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.

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)

'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.

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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