Dom绑定没有更新在ionic内容上滚动事件。

时间:2021-02-23 22:59:52

I want to do a progress bar when user scrolls down. When it's down all the way, the bar is 100% width.

当用户滚动时,我想做一个进度条。当它一直向下时,这个条是100%宽度。

http://codepen.io/hawkphil/pen/vOyPwN

http://codepen.io/hawkphil/pen/vOyPwN

HTML Ionic List Directive

HTML离子指令列表

    <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
  </head>

  <body ng-controller="MyCtrl">

    <ion-header-bar class="bar-positive">
      <div class="buttons">
        <button class="button button-icon icon ion-ios-minus-outline"
          ng-click="data.showDelete = !data.showDelete; data.showReorder = false"></button>
      </div>
      <h1 class="title">Ionic Delete/Option Buttons</h1>
      <div class="buttons">
        <button class="button" ng-click="data.showDelete = false; data.showReorder = !data.showReorder">
            Reorder
        </button>
      </div>
      <!--div class="progress" ng-style='{"width": pos.x}'></div-->
      <div class="progress" ng-style='getXObj()'></div>
    </ion-header-bar>

    <ion-content on-scroll="isScrolling()">

      <!-- The list directive is great, but be sure to also checkout the collection repeat directive when scrolling through large lists -->

      <ion-list show-delete="data.showDelete" show-reorder="data.showReorder">

        <ion-item ng-repeat="item in items" 
                  item="item"
                  href="#/item/{{item.id}}" class="item-remove-animate">
          Item {{ item.id }}
          <ion-delete-button class="ion-minus-circled" 
                             ng-click="onItemDelete(item)">
          </ion-delete-button>
          <ion-option-button class="button-assertive"
                             ng-click="edit(item)">
            Edit
          </ion-option-button>
          <ion-option-button class="button-calm"
                             ng-click="share(item)">
            Share
          </ion-option-button>
          <ion-reorder-button class="ion-navicon" on-reorder="moveItem(item, $fromIndex, $toIndex)"></ion-reorder-button>
        </ion-item>

      </ion-list>

    </ion-content>

  </body>
</html>

JS

JS

angular.module('ionicApp', ['ionic'])

.controller('MyCtrl', function($scope, $ionicScrollDelegate) {

  $scope.data = {
    showDelete: false
  };

  $scope.edit = function(item) {
    alert('Edit Item: ' + item.id);
  };
  $scope.share = function(item) {
    alert('Share Item: ' + item.id);
  };

  $scope.moveItem = function(item, fromIndex, toIndex) {
    $scope.items.splice(fromIndex, 1);
    $scope.items.splice(toIndex, 0, item);
  };

  $scope.onItemDelete = function(item) {
    $scope.items.splice($scope.items.indexOf(item), 1);
  };

  $scope.items = [
    { id: 0 },
    { id: 1 },
    { id: 2 },
    { id: 3 },
    { id: 4 },
    { id: 5 },
    { id: 6 },
    { id: 7 },
    { id: 8 },
    { id: 9 },
    { id: 10 },
    { id: 11 },
    { id: 12 },
    { id: 13 },
    { id: 14 },
    { id: 15 },
    { id: 16 },
    { id: 17 },
    { id: 18 },
    { id: 19 },
    { id: 20 },
    { id: 21 },
    { id: 22 },
    { id: 23 },
    { id: 24 },
    { id: 25 },
    { id: 26 },
    { id: 27 },
    { id: 28 },
    { id: 29 },
    { id: 30 },
    { id: 31 },
    { id: 32 },
    { id: 33 },
    { id: 34 },
    { id: 35 },
    { id: 36 },
    { id: 37 },
    { id: 38 },
    { id: 39 },
    { id: 40 },
    { id: 41 },
    { id: 42 },
    { id: 43 },
    { id: 44 },
    { id: 45 },
    { id: 46 },
    { id: 47 },
    { id: 48 },
    { id: 49 },
    { id: 50 }
  ];

  $scope.pos = {x:'21.41982072480742%', xObj: {}};
  $scope.isScrolling = function() {
    console.log($ionicScrollDelegate.getScrollPosition());
    $scope.pos.x = ($ionicScrollDelegate.getScrollPosition().top / 2204 * 100) + '%';
    console.log($scope.pos.x);
    //$scope.getXObj();
  }

  $scope.getXObj = function() {
    console.log({"width": $scope.pos.x});
    return {"width": $scope.pos.x + "%"};
  }

});

CSS

CSS

body {
  cursor: url('http://ionicframework.com/img/finger.png'), auto;
}

.progress {
  height: 2px;
  background: red;
}

I have tried two things:

我试过两件事:

ng-style='{"width": pos.x}'

ng-style = ' {“宽度”:pos.x }’

and

ng-style='getXObj()'

ng-style = ' getXObj()”

But didn't bind to provide the red bar expanding while I scroll down.

但是当我向下滚动的时候没有绑定提供扩展的红色条。

I have read this question:

我读过这个问题:

Angular.js ng-style won't bind value

角。js ng样式不会绑定值

I am running out of idea why although the console is showing the right percentage value.

虽然控制台显示的百分比值是正确的,但是我不知道为什么。

Any clue why this doesn't work?

你知道这为什么不奏效吗?

1 个解决方案

#1


2  

Seems like a possible ionic bug. It appears to call the function alright on the scroll event of the element but does not call digest cycle to update DOM.

似乎是一个可能的离子虫。它似乎在元素的滚动事件上调用函数,但不调用摘要循环来更新DOM。

Snippet from ionic:

从离子片段:

var scrollFunc = function(e) {
    var detail = (e.originalEvent || e).detail || {};
    $scope.$onScroll && $scope.$onScroll({  //<-- Here it gets called but it is outside of angular context
      event: e,
      scrollTop: detail.scrollTop || 0,
      scrollLeft: detail.scrollLeft || 0
    });
  };

  $element.on('scroll', scrollFunc); //event registration

So as a possible fix you could do a scope.$evalAsync which would be probably safer option.

作为一种可能的修复,你可以做一个范围。$evalAsync可能是更安全的选项。

$scope.isScrolling = function() {
    $scope.pos.x = ($ionicScrollDelegate.getScrollPosition().top / 2204 * 100) + '%';
    $scope.$evalAsync(angular.noop);
  }

Demo

演示

#1


2  

Seems like a possible ionic bug. It appears to call the function alright on the scroll event of the element but does not call digest cycle to update DOM.

似乎是一个可能的离子虫。它似乎在元素的滚动事件上调用函数,但不调用摘要循环来更新DOM。

Snippet from ionic:

从离子片段:

var scrollFunc = function(e) {
    var detail = (e.originalEvent || e).detail || {};
    $scope.$onScroll && $scope.$onScroll({  //<-- Here it gets called but it is outside of angular context
      event: e,
      scrollTop: detail.scrollTop || 0,
      scrollLeft: detail.scrollLeft || 0
    });
  };

  $element.on('scroll', scrollFunc); //event registration

So as a possible fix you could do a scope.$evalAsync which would be probably safer option.

作为一种可能的修复,你可以做一个范围。$evalAsync可能是更安全的选项。

$scope.isScrolling = function() {
    $scope.pos.x = ($ionicScrollDelegate.getScrollPosition().top / 2204 * 100) + '%';
    $scope.$evalAsync(angular.noop);
  }

Demo

演示