簡體   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