数字输入中的ng-model不影响范围

时间:2015-11-20 15:55:02

标签: javascript angularjs

ng-model不会影响范围,我无法理解为什么。

以下是我的大部分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>

以下是我的大多数控制人员:

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

        $scope.amount = '';

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

      }]);
})();

我不认为这可能是一个范围问题。没有嵌套控制器。路由处理如下:

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

无论我在文本框中写什么,$ scope.amount仍然是未定义的。可能导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:4)

更新回答:

跟进Sunil D和Dayan Moreno对这个问题的评论,将原始值抛在对象中是最好的方法,并且避免在代码中使用$ parent:

ng-model='object.amount'

只要子范围中不存在对象,它将引用父范围中找到的对象变量并适当地设置金额值。

原始回答:

还没有使用过ion,我会假设ionView或ionContent是一个创建自己范围的指令。

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

ng-model='$parent.amount'
相关问题