【问题标题】:"ng-click" not working out of "ion-content" in Ionic framework“ng-click”在 Ionic 框架中无法使用“ion-content”
【发布时间】:2016-07-14 01:18:33
【问题描述】:

我在基于 ionic 的应用程序中有一个带有 ng-model 属性的输入框。 ion-content 标签内的代码:

<ion-content class="padding">
  <input ng-model="myNumber" type="number" placeholder="Enter number">
</ion-content>

footer-bar 我有这个:

<div class="bar bar-footer bar-balanced" style="background-color: #00368C !important; color: #fff;">
    <div class="title">
      <button ng-click="submit(myNumber)" class="button button-clear">Submit</button>
    </div>
</div>

警报结果是未定义

注意:当我将 button 放入 ion-content 时,它工作正常。 (这意味着js代码可以正常工作)

有什么想法吗?

【问题讨论】:

    标签: javascript angularjs ionic-framework ionic-view


    【解决方案1】:

    更好、更简单的解决方案,使用&lt;ion-header-bar&gt;&lt;ion-footer&gt; 并将所有固定内容放在其中 - 然后将所有代码保存在同一个控制器中。

    引用自:https://stackoverflow.com/a/51785866/2036221

    【讨论】:

      【解决方案2】:

      问题背后的原因是 ion-content directive does create a child scope 原型继承自父范围。因此,通过将myNumber 放入输入ng-model 确实会添加到ion-content 的范围内,这与控制器myNumber 数字范围变量不同。

      要使其工作,您需要在定义ng-model 时遵循dot rule,以便遵循原型继承规则(它适用于引用类型变量)。 因此,在控制器中创建一个新对象并将所有 ng-model 变量分配给它。如下所示

      $scope.model = {};
      

      然后将您想要使用的所有属性放在视图上,例如 $scope.model.myNumber 并在视图上使用它时使用它,例如 model.myNumber

      标记

      <ion-content class="padding">
        <input ng-model="model.myNumber" type="number" placeholder="Enter number">
      </ion-content>
      
      <div class="bar bar-footer bar-balanced" style="background-color: #00368C !important; color: #fff;">
          <div class="title">
            <button ng-click="submit(model.myNumber)" class="button button-clear">Submit</button>
          </div>
      </div>
      

      详细解释可见this answer

      更优雅的做法是使用controllerAs 方法。

      【讨论】:

      • @vahidnajafi 很高兴为您提供帮助.. 谢谢 :-)
      • @PankajParkar - 我需要做类似的事情,将 DIV 固定到屏幕顶部并允许 ion-content 在其后面滚动。但我需要 DIV 中的按钮和输入传递给控制ion-content 视图的控制器。上面的例子如何适应我的需要?我正在使用 Ionic v1
      • @PankajParkar - 在您的示例中,如果您将 id="divID" 添加到 DIV,您如何从管理 ion-content 的控制器使用 document.getElementById('divID') 访问 DOM ?当我尝试操纵 DIV 的样式时,我收到一个 JS 错误 cannot read property style of null - 我不认为 DOM 元素受到每个离子视图的限制,但它似乎是。
      猜你喜欢
      • 2018-08-09
      • 2016-05-14
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 2019-06-13
      相关资源
      最近更新 更多