0

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

我目前正在使用一个按钮来显示消息并使用 ng-show 来检查“第一次”标志;此标志将在本地存储为布尔值“真”或“假”值,我正在使用 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 次。它不在任何类型的循环中,我确实有下面元素列表的代码逻辑,但是该列表独立于按钮,反之亦然。

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

4

1 回答 1

0

您可以创建一个工厂/服务并设置标志来检查它是否是第一次访问。

于 2016-08-04T10:18:39.830 回答