【发布时间】:2017-04-25 11:45:15
【问题描述】:
我是 angularjs 的初学者。 我想在点击添加图标时动态添加一个 div。我已经为此做了一些脚本。
HTML 部分:
<div ng-show="child" class="childDiv" data-ng-repeat="choice in choices track by $index">
<label for="childname" class="childname" >ServerName </label>
<input ng-model="serverinfo.childname" type="text" required="required" placeholder="name"/>
<label for="childname" class="childname" >Fullname</label>
<input ng-model="serverinfo.childfullname" type="text" required="required" placeholder="fullname"/>
<label for="childname" class="childname" >Mandatory Field</label>
<input ng-model="serverinfo.field" type="text" required="required" placeholder="city/county.."/>
<label for="childname" class="childname" >Field values</label>
<input ng-model="serverinfo.value" type="text" required="required" placeholder=""/>
<i ng-click="removechild()" ng-show="$last" style="padding-left:16em;" class="material-icons">remove</i>
</div>
<i ng-click="addchild()" style="padding-left:16em;" class="material-icons">add</i>
JS部分:
$scope.addchild = function() {
// $scope.child = true;
var newItemNo = $scope.choices.length+1;
$scope.choices.push({'id':'choice'+newItemNo});
};
$scope.removechild = function() {
var lastItem = $scope.choices.length-1;
$scope.choices.splice(lastItem);
};
我的问题就像我在文本框中输入的任何内容一样,它会自动复制到下一组。谁能解决这个问题。
【问题讨论】:
-
为什么总是将值绑定到
serverinfo?你不应该把它绑定到choice in吗? -
谢谢你的建议,我会努力实现的
-
您应该知道,在此更改之后,
$scope.serverinfo中将没有任何内容。如果您想在控制器中使用serverinfo,那么您应该使用serverinfo[ $index ].yourPropertyName- 这将为您提供一个数组,其中$scope.serverinfo数组中元素的属性分别与$scope.choices中的选项匹配 -
谢谢你的工作
-
阿隆你能把这个作为答案发帖吗
标签: html angularjs angularjs-ng-repeat angularjs-ng-model