Angular - 动态增加ng-model名称

时间:2017-10-11 06:07:32

标签: javascript angularjs

我有以下ng-model:

<input class="text__Right" type="datetime" ng-model="formData.MODELNAME"  ng-init="formData.MODELNAME =  testDate" readonly>

我还有一个名为$scope

$blockCounter变量
<div class="list list-inset block" id="block{{ blockCounter }}">
    <div class="item item-divider">
       Test
        <div class="dynamic-add">
            <a class="button icon-left button-dark button-small button__Icon_Vertical add" href="javascript: void(0)" ng-click="copy($event)" data-block="block{{ blockCounter }}">
                <i class="icon ion-ios-plus-outline"></i> New
            </a>
            <a class="button icon-left button-dark button-small button__Icon_Vertical remove" href="javascript: void(0)" ng-click="remove($event)" data-block="block{{ blockCounter }}">
                <i class="icon ion-ios-minus-outline"></i> Remove
            </a>
            <a class="button icon-left button-dark button-small button__Icon_Vertical" href="javascript: void(0)" ion-datetime-picker ng-model="datetimeValue" ng-click="getFormDataModel('MODELNAME{{ blockCounter }}')">
                <i class="icon ion-ios-clock-outline"></i> Date
            </a>
        </div>
    </div>
</div>

使用copy()事件,我将原始元素克隆到新元素中。它有效,但我无法动态增加ng-model名称。所以我想要以下内容:

例如,如果$blockCounter = 2:

<input class="text__Right" type="datetime" ng-model="formData.MODELNAME2"  ng-init="formData.MODELNAME2 =  testDate" readonly>

我尝试以下操作,但不能正常工作:

<input class="text__Right" type="datetime" ng-model="formData.MODELNAME{{ blockCounter }}"  ng-init="formData.MODELNAME{{ blockCounter }} =  testDate" readonly>

或:

<input class="text__Right" type="datetime" ng-model="formData.MODELNAME[blockCounter]"  ng-init="formData.MODELNAME[blockCounter] =  testDate" readonly>

更新 - 控制器操作

$scope.copy = function($event) {
        if ($event.currentTarget && $event.currentTarget.attributes['data-block'])
            var block = $event.currentTarget.attributes['data-block'].value;

            var original = document.getElementById('block' + $scope.blockCounter);
            var cloneBlock = document.getElementById('cloneBlockA');
            var clone = original.cloneNode(true);

            // var original = angular.element( document.querySelector( '#block' + $scope.blockCounter) );
            // var wEl = angular.element( document.querySelector( '#block' + $scope.blockCounter) );
            // var compiledElement = $compile(wEl.clone())($scope);

            console.log(original);

            // var clone = original.cloneNode(true);
            // var clone = $compile(original.cloneNode(true))($scope);
            $scope.blockCounter++;
            clone.id = "block" + $scope.blockCounter;
            cloneBlock.appendChild(clone);
            console.log($scope.blockCounter);

            switch($scope.blockCounter) {
                case 0:
                    $scope.addButton = true;
                    $scope.removeButton = false;
                    break;
                case 1:
                    $scope.addButton = true;
                    $scope.removeButton = true;
                    break;
                case 2:
                    $scope.addButton = true;
                    $scope.removeButton = true;
                    break;
                case 3:
                    $scope.addButton = false;
                    $scope.removeButton = true;
                    break;
                default:
                    $scope.addButton = true;
                    $scope.removeButton = false;
            }

            // var compiledElement = $compile(wEl.clone())($scope);
        // iEl.append(compiledElement);
        };

如何动态增加ng-model名称?

1 个答案:

答案 0 :(得分:1)

实际上,当块计数器增加时,在input元素处分配的模型会被更改,但问题是新模型的值未定义,因为 ng-init仅在元素第一次渲染时运行。要解决此问题,每次blockCounter增加时都必须为新模型分配值。

  $scope.testDate = new Date();
  $scope.blockCounter = 0;

  $scope.formData = {
    MODELNAME:{}
  };

  $blockCounter = 0;

  $scope.onBlockCounterIncrease = function() {
    $scope.blockCounter++;
    $scope.formData.MODELNAME[$scope.blockCounter] = $scope.testDate;
  }

这是working fiddle