NG-模型数字输入不影响范围 [英] ng-model in numeric input does not affect scope

查看:98
本文介绍了NG-模型数字输入不影响范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NG-模式不影响的范围,我不明白为什么。

ng-model does not affect the scope and I can't understand why.

下面是我的大多数HTML的:

Here is most of my HTML:

<ion-view view-title="{{ inputType | uppercase | translate }}">

  <ion-content class="padding">

    <div class="list">
      <label class="item item-input">
        <input ng-model="amount" class="text-align-center" type="number" placeholder="{{ 'AMOUNT' | translate }}">
      </label>
    </div>

    <button class="button button-block button-stable" ng-click="okAmount()">{{ 'SAVE' | translate }}</button>

  </ion-content>
</ion-view>

下面是我的大多数控制器:

Here is most of my controller:

(function(){
  angular.module('mb')
    .controller('EnterAmountCtrl', ['$scope', '$state', '$filter', '$translate',
      function($scope, $state, $filter, $translate) {

        $scope.amount = '';

        $scope.okAmount = function() {
          console.log($scope.amount);
        };

      }]);
})();

我不认为这可能是一个作用域的问题。有没有嵌套的控制器。路由是这样处理的:

I don't think that this can be a scoping issue. There are no nested controllers. Routing is handled like this:

    $stateProvider.state('enter-amount', {
      url: '/enter-amount/:inputType',
      templateUrl: 'templates/enter-amount.html',
      controller: 'EnterAmountCtrl'
    });

不管我在文本框中写,$ scope.amount仍然不确定。什么导致此?

Regardless of what I write in the text box, $scope.amount remains undefined. What might be causing this?

推荐答案

更新答案:

跟进苏尼尔D和大雁莫雷诺的意见的问题,抛出一个物体内部的原始值是最好的方法,并避免了使用$父在code:

Following up on Sunil D and Dayan Moreno's comments to the question, throwing the primitive value inside an object is the best approach, and avoids having to use $parent in your code:

ng-model='object.amount'

只要对象不符合儿童的范围存在,它会参考父范围中找到的对象变量并设置适当的金额值。

As long as object doesn't exist in the child scope, it will refer to the object variable found in the parent scope and set the amount value appropriately.

原来的答复:

已经不使用离子然而,我会假设,无论是ionView或ionContent是创建自己的范围指令。

Having not used ion yet, I would assume that either ionView or ionContent is a directive which creates its own scope.

您在设置子作用域的value属性,然后试图访问父作用域的value属性。访问父作用域的金额值可能会解决这一问题为您提供:

You're setting the value property of a child scope, then trying to access the value property of the parent scope. Accessing the parent scope's amount value might solve the issue for you:

ng-model='$parent.amount'

这篇关于NG-模型数字输入不影响范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆