0
我有以下的NG-模型NG-型號名稱:角 - 動態增加
<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>
與複製()事件中,我克隆將原始元素整合到新的元素中。它可以工作,但我不能動態增加ng模型名稱。所以,我想以下幾點:
例如,如果$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
名稱?
複製如何工作?你能分享你的控制器代碼並提供更多的細節嗎?我不明白你需要做什麼。 – Korte
我剛剛更新了代碼w /控制器 – wpdaniel