【问题标题】:OnsenUI, Angular and refreshing a UI component after an alert dialogOnsenUI、Angular 和在警报对话框后刷新 UI 组件
【发布时间】:2016-05-13 12:21:44
【问题描述】:

我正在开发一个使用 Monaca、OnsenUI 和 Angular 的移动应用程序。在一个页面中,我有一个可点击项目的列表;当用户点击一个条目时,应该会出现一个对话框来输入一个值。该值应显示在点击的列表项中。它确实有效,但在我再次点击列表项之前不会刷新该值,即使只是取消对话框也是如此。我希望在用户关闭对话框后立即更新显示的值。

这是一个 HTML sn-p:

<ons-page>

  <ons-row style="margin-top: 10px; text-align: center;">
    <ons-col ng-controller="NewSubmissionController">
      <ons-list id="materialList" style="margin-top: 12px;">
        <ons-list-header>Pick the Materials</ons-list-header>
        <ons-list-item modifier="tappable" ng-click="doPickMaterial('ferrous')">
          Ferrous (<span ng-bind="materialCounters['ferrous']"></span>)
        </ons-list-item>
        <ons-list-item modifier="tappable" ng-click="doPickMaterial('non-ferrous')">
          Metal, non-ferrous ({{materialCounters['non-ferrous']}})
        </ons-list-item>
        <ons-list-item modifier="tappable" ng-click="doPickMaterial('plastic')">
          Plastic ({{materialCounters['plastic']}})
        </ons-list-item>
        <ons-list-item modifier="tappable" ng-click="doPickMaterial('other')">
          Other ({{materialCounters['other']}})
        </ons-list-item>
      </ons-list>
    </ons-col>
  </ons-row>

</ons-page>

这里是控制器:

app.controller('NewSubmissionController', function($scope) {

  $scope.materialCounters = {
    'ferrous': 0,
    'non-ferrous': 0,
    'plastic': 0,
    'other': 0
  };

  $scope.doPickMaterial = function(matType) {
    ons.notification.prompt({
      title: 'Material: ' + matType,
      message: 'Please enter number of items',
      cancelable: true,
      animation: 'slide',
      callback: function(newCt) {
        alert('Input value: *' + newCt + '*');
        if (newCt != null) {
          if (newCt == '') { //  An empty input will be taken as zero
            newCt = 0;
          }
          $scope.materialCounters[matType] = n;
          alert('Count for ' + matType + ': ' + $scope.materialCounters[matType]);
        }
      }
    });
  };
});

我尝试使用 ng-bind 和双花括号表示法,但行为没有区别。

【问题讨论】:

    标签: javascript angularjs onsen-ui monaca


    【解决方案1】:

    基本上,如果您想更新视图,您可以手动调用$scope.apply(),一切都会自行更新。可能有一些“更智能”的解决方案,但由于您使用的是ons.notification.prompt,这看起来是最简单的解决方案。

    【讨论】:

    • 它有效。我不确定我是否理解为什么需要显式调用它,因为当模型改变时视图会自行更新?
    • 好吧,基本上它只在发生对角度感兴趣的事情时才会自动检查值。例如,如果执行一些角度逻辑,它将知道要检查。然而,在这种情况下,角度本身没有做任何事情。一个简单的例子是如果有一些ng-click 或类似的。但是,如果您只是执行您的逻辑,它将不会自动更新。话虽如此,我想在这种情况下,如果 Onsen 自己更新这些东西会很好,因为这是一种常见的情况。
    猜你喜欢
    • 2023-01-29
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 2013-06-26
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多