如何在angularJS

时间:2017-11-09 11:31:50

标签: angularjs input

我正在尝试编辑视图和输入字段的读取视图。 目标是编辑一个没有任何格式的数字,但用一些特定的间距和小数位显示它。

要做到这一点,我使用了2个字段,但在任何给定时间只显示一个字段。

我还需要保存输入值,如果它已被更改,那么使用ng-change和ng-model-options =" {updateOn:' blur'}"因此,我不会在每次按键时都执行保存请求。

问题是我一直收到这个错误"无法读取属性' $$阶段' of null"

我在这里留下了我的代码的简化示例(虽然在这个网站上似乎没有显示错误,只是错过了item.isInEditMode更新)



var app = angular.module('Appp', []);
        app.controller('metadataCtrl', function ($scope, $http) {
            $scope.vm = { myItems : {} }

            $scope.vm.myItems = [ {type: "a", value: 34.5},
                                  {type: "a", value: 12.5},
                                  {type: "b", value: 3.15} ];

            $scope.setEditMode = function ( item, val ) {
               item.isInEditMode = val;
            }

            $scope.saveValue = function ( rowRate ) {
                           //do something
            }
        });

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form ng-app="Appp" name="myForm" ng-controller="metadataCtrl">
   
          <div ng-repeat="item in vm.myItems">
              <input type="number"
                   ng-if="!item.isInEditMode"
                   ng-focus="setEditMode(item, true)"
                   ng-value="item.value | number : 2"
                   step="0.01"
                   max="99999999999999"
                   min="0"
                   step-message="{name} can only have a maximum of 2 decimal places"/>
            <input type="number"
                   ng-if="item.isInEditMode"
                   step="0.01"
                   max="99999999999999"
                   min="0"
                   step-message="{name} can only have a maximum of 2 decimal places"
                   ng-blur="setEditMode(item, false)"
                   ng-model="item.value"
                   ng-change="saveValue( item )"
                   ng-model-options="{updateOn: 'blur'}"/>
          </div>
   </form>
&#13;
&#13;
&#13;

编辑: 如果我只使用ng-blur并检查是否输入。$更改以检查我是否调用了我的函数确实克服了$$阶段错误。

但我仍然无法关注隐藏的输入

1 个答案:

答案 0 :(得分:0)

我只做了一个小改动,我希望这是你所期待的。如果不是,我会再试一次。

<form name="myForm" ng-controller="MyCtrl">

          <div ng-repeat="item in vm.myItems">
              <input type="number"
                   ng-if="!item.isInEditMode"
                   ng-focus="setEditMode(item, true)"
                   step="0.01"
                   max="99999999999999"
                   min="0"                   
                   step-message="{name} can only have a maximum of 2 decimal places"
                   ng-model="item.value"
                   disabled
                   />
            <input type="number"
                   ng-if="item.isInEditMode"
                   step="0.01"
                   max="99999999999999"
                   min="0"
                   step-message="{name} can only have a maximum of 2 decimal places"
                   ng-blur="setEditMode(item, false)"
                   ng-model="item.value"
                   ng-change="saveValue( item )"
                   ng-model-options="{updateOn: 'blur'}"/>
          </div>
   </form>