繁体   English   中英

第一次进入页面时如何显示按钮?

[英]How to display a button when entering a page for the first time only?

我正在使用AngularJS,HTML,Javascript和Ionic来找出正确的方法,以便当用户首次进入应用程序中的特定页面时向用户显示消息。 此后不应显示此消息。

我目前正在使用按钮来显示消息,并使用ng-show检查“首次”标志; 此标志将以布尔值“ true”或“ false”的形式存储在本地,而我使用window.localStorage进行存储。 我想知道这种方法是否正确,如果正确,那么控制器中存在的第一次标记的代码段如下所示:

$scope.getFT = function(){                      
    return window.localStorage['firstTime'] || 0;
};

$scope.setFT = function(value){
    window.localStorage['firstTime'] = 1;                       
};

$scope.checkFT = function(){
    // Checks whether or not this is the first time (FT) the user entered this page
    if(($scope.getFT() === 0 || $scope.getFT() === 'undefined')
    {// This is the first time navigating in this page, show message & set local flag to true
        console.log("checkFT: true, value of FT: " + window.localStorage['FT'] + ', now setting FT to true');
        $scope.setFT(1);
        return true;
    }
    else
    {// Not first time navigating through here, do not show first time message
        console.log("checkFT: false, value of FT: " + window.localStorage['FT']);
        return false;
    }
};

在我的页面上,我有一个下面的按钮,下面有更多代码,例如:

<ion-header-bar>
    <!-- some code here -->
<ion-header-bar>

<ion-content>
    <!-- First Time Button -->
    <button ng-show='checkFT()' class="firstTime">
        <img src=".../img/firstTimeImage.png"/>
    </button>

    </ion-scroll>
        <ion-list ng-repeat="i in array">
        <!-- . . . . more code below . . . . -->
        </ion-list>
    </ion-scroll>

</ion-content>

在调试窗口中,我看到我的应用程序识别出这是我第一次进入页面,但是由于某种原因,该函数在页面加载完成之前被调用了约9次。 它不是任何形式的循环,对于下面的元素列表,我确实具有代码逻辑,但是该列表独立于按钮,反之亦然。

这就是为什么我想知道,我到底在做什么错? 我的逻辑仅仅是错误的,我的方法是完全错误的,还是我是正确的,而我只是在多次处理浏览器加载?

您可以创建工厂/服务并设置标志以检查其是否为首次访问。

暂无
暂无

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

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