AngularJS ng - 对输入文本奇怪行为的重复,ng - 模型

时间:2017-01-23 03:00:01

标签: javascript angularjs angularjs-ng-repeat angularjs-ng-model

我是AngularJS的新手,我尝试执行以下代码

var app = angular.module('SomeApp', []);

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

   $scope.init = function(){
     $scope.chargableDescription = [""];
     $scope.chargablePrice = [];
     $scope.chargableQuantity = [];
     $scope.chargableTotal = [];

   }

   $scope.chargableInput = function($last){

     if ( $last ) {
       $scope.chargableDescription.push([""]);
     }
   }
});

基本上,我想在这里实现的是当用户在最后一个chargableDescription字段上输入内容时插入整组输入。

      <div class="chargable-group" ng-repeat="item in chargableDescription" >

            <div class="col-md-3">
                 <label class="form-control-label" for="l2" id="chargable-label">Chargable Item</label>
            </div>

            <div class="col-md-9" id="chargable-header">
                 <textarea name="chargable[]" class="form-control dynamic chargable" placeholder="Chargable Description" ng-model="chargableDescription[$index]" ng-keypress="chargableInput($last)"> </textarea>
                 <br>
                 <input type="number" class="form-control" step="0.01" name="chargable-price-1" placeholder="Chargable Price" ng-model="chargablePrice[$index]">
                 <br>
                 <input type="number" class="form-control" name="chargable-quantity-1" placeholder="Chargable Quantity" ng-model="chargableQuantity[$index]">
                 <br>
                 <input type="number" class="form-control" step="0.01" name="chargable-total-1" placeholder="Chargable Total" readonly ng-model="chargableTotal[$index]" >
            </div>

      </div>

它有诀窍,但是,我想知道为什么当我在textarea上做任何输入时,一旦我输入一个字符,光标就会消失。 如何删除此行为以及导致此行为的因素是什么?

更新:

解决 我添加了ng-model-options = {updateOn:'blur'},似乎它解决了这个问题

1 个答案:

答案 0 :(得分:0)

它对我有用https://plnkr.co/IV9t4fhln5v3l81NHaPC

你的Angular版本是什么?

(function() {

  'use strict';

  var app = angular.module('SomeApp', []);

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

    $scope.init = function() {
      $scope.chargableDescription = [""];
      $scope.chargablePrice = [];
      $scope.chargableQuantity = [];
      $scope.chargableTotal = [];

    }

    $scope.chargableInput = function($last) {

      if ($last) {
        $scope.chargableDescription.push([""]);
      }
    }

    $scope.init();
  });


})();