作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的表单中,我有一些输入,即我的自定义指令和一些文本字段。另外,我还有按钮:第一个用于“向表单添加新输入”,它绑定(bind)到 Controller 中的函数 $scope.AddNewItem()
,第二个用于“从表单中删除输入”,它绑定(bind)到函数$scope.RemoveItem(index_of_item)
。当我在文本字段中键入并按 Enter 键时出现问题 - 输入值被删除,因为 $scope.RemoveItem(index_of_item)
被称为自身。我确信我不会调用它,因为即使调用堆栈也没有显示我可以执行此操作的位置。有人有同样的问题吗?
<div class="form-group " ng-repeat="(i, user) in users">
<div class="col-sm-5" style="padding-right: 0px; margin-top: 0px">
<div typeahead ng-model="users[i]"> </div>
</div>
<div class="col-sm-2 ng-scope" style="padding-left: 5px; margin-top: 0px">
<button ng-click="removeUserInput(i)" class="btn pull-right m-t-n-xs remove-input">
<i>
<span class="glyphicon glyphicon-minus" style="color: white;"></span>
</i>
</button>
<button ng-click="addUserInput()" class="btn pull-right m-t-n-xs add-input">
<i>
<span class="glyphicon glyphicon-plus" style="color: white;"></span>
</i>
</button>
</div>
</div>
我的 Controller 代码:
$scope.addUserInput = function () {
$scope.users.push({});
};
$scope.removeUserInput = function (index) {
if ($scope.users.length > 1) {
$scope.users.splice(index, 1);
}
else if ($scope.users.length == 1) {
$scope.users[0] = {};
}
};
最佳答案
只需使用以下代码更新您的代码...问题是您错过了按钮的 type="button"。
<div class="form-group " ng-repeat="(i, user) in users">
<div class="col-sm-5" style="padding-right: 0px; margin-top: 0px">
<div typeahead ng-model="users[i]">
</div>
<div class="col-sm-2 ng-scope" style="padding-left: 5px; margin-top: 0px">
<button type="button" ng-click="removeUserInput(i)" class="btn pull-right m-t-n-xs remove-input">
<i>
<span class="glyphicon glyphicon-minus" style="color: white;"></span>
</i>
</button>
<button type="button" ng-click="addUserInput()" class="btn pull-right m-t-n-xs add-input">
<i>
<span class="glyphicon glyphicon-plus" style="color: white;"></span>
</i>
</button>
</div>
</div>
</div>
关于javascript - AngularJS 函数以表单调用自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45012648/
我正在寻找一种快速方法来使 pandas 数据帧在 x 中单调。 我当前的解决方案如下: def make_monotonic(df, cols=None): """make df monot
CLOCK_REALTIME 的一个问题是它不是单调的,如果发生 NTP 同步,时间可能会倒退。 像下面这样的事情让它变得单调是否安全? struct timespec GetMonotonicTim
我是一名优秀的程序员,十分优秀!