简体   繁体   English

ionic-angularjs-在Android设备上重新打开应用程序后未呈现列表

[英]ionic - angularjs - list not rendering after reopening app on android device

I have this mobile app where it lists a record of encoded transport/taxi receipt. 我有这个移动应用程序,其中列出了编码的运输/出租车收据的记录。 The first page seen when opening the app is the list of receipt. 打开应用程序时看到的第一页是收据列表。

templates/list-view.html 模板/列表view.html

<ion-item class="card item item-light" ng-click="edit({{receipt.id}})" ng-repeat="receipt in receipts|orderBy:'receiptDate'">
   ...
</ion-item>
   ...
<ion-footer-bar class="bar-subfooter taxi-subfooter">
<div class="row">
    <div class="col">
        <button ng-click="addRecord(-1)" class="button button-block"><i class="icon ion-plus-round"></i></button>
    </div>
</div>

if I click the item or if I click the button with addRecord(-1) , it will go to the add/edit receipt page: 如果单击该项目,或者单击带有addRecord(-1)的按钮,它将转到添加/编辑收据页面:

templates/edit-receipt.html 模板/编辑receipt.html

<ion-content padding="true" class="has-header has-footer taxi-has-subfooter">
    <div class="table">
        <div class="row">
            <div class="col title">Paper receipt provided?</div>
            <div class="col taxi-col">
                <label class="toggle toggle-balanced">
                    <input type="checkbox" ng-model="bindReceipt.hasReceipt">                       
                    <div class="track taxi-track">
                        <div class="handle taxi-handle"></div>
                    </div>
                </label>
                <input type="hidden" ng-model="bindReceipt.id" />
            </div>
        </div>
        <div class="row">
            <div class="col title">Date</div>
            <div class="col taxi-col"><input type="date" ng-model="bindReceipt.receiptDate" /></div>
        </div>
        <div class="row">
            <div class="col title">Vendor</div>
            <div class="col taxi-col"><input type="text" placeholder="Name of the Taxi" ng-model="bindReceipt.taxiVendor" /></div>
        </div>
        <div class="row">
            <div class="col title">Fare</div>
            <div class="col taxi-col"><input type="tel" placeholder="0.00" ng-model="bindReceipt.fare" /></div>
        </div>
    </div>
</ion-content>
<ion-footer-bar class="bar-subfooter taxi-subfooter">
        <div class="row"> <!-- update() functions also as add -->
            <div class="col taxi-col"><button class="button button-block" ng-click="update()">{{transType}}</button></div>
        </div>
    </ion-footer-bar>

What I did in its behavior for back button of the phone is if the state is on the add/edit page, it will just go back to the list page, and if the state is on the list page, no matter how many times the user will go from/to list and edit pages, it should exit the app (no going back of the app history). 我对手机的“后退”按钮所做的操作是,如果状态位于添加/编辑页面上,它将仅返回到列表页面,并且如果状态位于列表页面上,则无论返回多少次。用户将进入/进入列表并编辑页面,它将退出应用程序(不返回应用程序历史记录)。 So, I used navigator.app.exitApp() for the backbutton on the list page. 因此,我将navigator.app.exitApp()用于列表页面上的后退按钮。

templates/list-view.html uses ReceiptsCtrl controller so I put there my event listener for backbutton: templates / list-view.html使用ReceiptsCtrl控制器,因此我在其中放置了用于backbutton的事件监听器:

app.controller('ReceiptsCtrl', function($window, $scope, $filter, $state, $http, $ionicPopup, Receipts) {

    $scope.receipts = Receipts.getReceipts();

    $scope.historyBack = function(evt) {
        if(evt != null) {
          if(evt.preventDefault) {
            evt.preventDefault();
          }
        }

        var location = $window.location.href;
        if(location.charAt(location.length-1) === '/') {

            navigator.app.exitApp(); // exit the application
        } else {
            history.go(-1);
        }

      }

      document.addEventListener('backbutton', $scope.historyBack, false);

});

services.js services.js

.factory('Receipts', function($window, $filter) {

  var receipts = JSON.parse($window.localStorage.getItem('receipts')) || [];
  return {
    getReceipts: function() {return receipts;}

  }

});

Here's my problem, I have this scenario in my app after I build and run in android: 这是我的问题,在android中构建并运行后,我的应用中出现了这种情况:

  1. opening the app. 打开应用程序。 0 record initially 最初有0条记录
  2. add two items - 2 records in the list page 添加两个项目-列表页面中的2条记录
  3. currently on the list page then exit the app (navigator.app.exitApp() triggers) 当前在列表页面上,然后退出应用程序(navigator.app.exitApp()触发器)
  4. open the app again, 2 records is still there 再次打开应用,仍然有2条记录
  5. add one item - 3 records in the list page 添加一项-列表页面中的3条记录
  6. on the list page again then exit app 再次在列表页面上,然后退出应用
  7. open the app, missing list 打开应用, 缺少列表

Screenshot of the scenario here 场景的屏幕截图在这里

That is not happening when I do not use the backbutton event listener, but I find it annoying if it go through history swinging between list and edit page before exiting the app. 当我不使用backbutton事件侦听器时,不会发生这种情况,但是如果退出退出应用程序之前它在列表和编辑页面之间进行切换时,会感到很烦。

I use $window.localStorage for storing data then pass it on $scope.receipts for the list ng-repeat , I tried debugging it by inserting alert() with the receipt object fetched from localStorage as argument. 我使用$window.localStorage存储数据,然后将其$scope.receiptsng-repeat列表的$scope.receipts ,我尝试通过插入alert()与从localStorage获取的接收对象作为参数来调试它。 There is a value on the alert message but listing is not working. 警报消息中有一个值,但列表不起作用。 Another thing also is that it can still compute the total fare from the $scope.receipts (as stated in the screenshot also). 另一件事是,它仍然可以从$scope.receipts计算总票价(也如屏幕截图所示)。

Many thanks. 非常感谢。

I think I found the issue. 我想我找到了问题。 I have no idea how to replicate it on browser but I was able to catch it in Android Device Monitor 我不知道如何在浏览器上复制它,但是我能够在Android Device Monitor中捕获它

11-12 03:08:23.861: I/chromium(30444): [INFO:CONSOLE(139)] "Error: [ngRepeat:dupes] http://errors.angularjs.org/1.4.3/ngRepeat/dupes?p0=[Some data from my app URL encoded. It's too long]
11-12 03:08:23.861: I/chromium(30444):     at Error (<anonymous>)
11-12 03:08:23.861: I/chromium(30444):     at file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js:37:416
11-12 03:08:23.861: I/chromium(30444):     at file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js:309:261
11-12 03:08:23.861: I/chromium(30444):     at Object.fn (file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js:162:168)
11-12 03:08:23.861: I/chromium(30444):     at n.$digest (file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js:163:259)
11-12 03:08:23.861: I/chromium(30444):     at n.$apply (file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js:166:269)
11-12 03:08:23.861: I/chromium(30444):     at l (file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js:118:152)
11-12 03:08:23.861: I/chromium(30444):     at F (file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js:122:187)
11-12 03:08:23.861: I/chromium(30444):     at XMLHttpRequest.K.onload (file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js:123:220)", source: file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js (139)

I searched about ngRepeat:dupes - AngularJS doesn't allow duplicates and recommended to use track by to avoid errors like this. 我搜索了ngRepeat:dupes -AngularJS不允许重复,建议使用track by以避免此类错误。 So I changed my code and use it on ng-repeat . 所以我更改了代码,并在ng-repeat上使用了它。

With correct format, I wrote: 使用正确的格式,我写道:

ng-repeat="receipt in receipts|orderBy:'receiptDate'| filter:receiptFilter track by receipt.id"

I tried track by right after the orderBy but does not work. 我尝试在orderBy之后按orderBy track by但无法正常工作。 I found that it should be after a filter in order to work. 我发现它应该在filter之后才能起作用。 So I made a receiptFilter variable under my controller (something empty string like $scope.receiptFilter = ""; ) just to have something harmless in my result of ng-repeat . 所以我在控制器下创建了一个receiptFilter变量(诸如$scope.receiptFilter = "";类的空字符串)只是为了使ng-repeat结果无害。

Before resorting to track by receipt.id , I tried track by $index but the error persists. 在诉诸于track by receipt.id进行track by receipt.id之前,我尝试track by $index但是错误仍然存​​在。 I look for answers and found that it is not safe to use $index when using orderBy . 我寻找答案,发现使用orderBy时使用$index是不安全的。

In summary, when sorting ( orderBy ), use track by <a unique key> (not $index ). 总之,在排序时( orderBy ),请使用track by <a unique key> (不是$index )进行track by <a unique key>

I hope this answer will help and any more supplements on this one will be much appreciated. 我希望这个答案会有所帮助,对此的更多补充将不胜感激。 :) :)

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

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