javascript - 如何在单击angularjs中的按钮时将文本框值添加到数组并添加所需的功能
问题描述
您好,现在使用此代码,我可以将文本框值添加到数组中,并在将值添加到数组后清空文本框。现在我的问题是为文本框添加所需的验证。如果值存在于文本框或数组中,则应删除所需的验证。否则应该添加。请帮助我。
<div class="input-group">
<input ng-disabled="$parent.BuildModel.DisableControl" type="text" class="form-control textBoxStyle " name="Logpath" ng-model="$parent.BuildModel.Logpath" id="Logpath" required />
<span class="input-group-addon ">
<button ng-click="onclickfun();">
<i class="glyphicon glyphicon-plus"></i>
</button>
</span>
</div>
<span ng-disabled="$parent.BuildModel.DisableControl" class="help-block" name="LogPaths" id="LogPaths" ng-model="$parent.BuildModel.LogPaths" >{{LogPaths}}</span>
在控制器中
$scope.BuildModel.LogPaths = [];
$scope.onclickfun = function () {
if ($scope.BuildModel.LogPath.length < 0) {
Alertify.alert("Please enter log path for adding to list");
}
$scope.BuildModel.LogPaths.push($scope.BuildModel.LogPath);
console.log($scope.BuildModel.LogPaths);
$scope.BuildModel.LogPath = ClientConfig.EMPTY;
$scope.BuildModel.res = $scope.BuildModel.LogPaths.join(' ');
};
解决方案
你正在使用$parent
,但你不需要使用它。我为此创建了一个简单的示例。您可以在此处查看示例
html
<div ng-app ng-controller="LogController">
<input ng-model="inputData"></input>
<input type="submit" ng-click="addPath()" value="add"></input>
<div ng-repeat="log in logs">{{ log }}</div>
</div>
控制器
function LogController($scope) {
$scope.logs = [];
$scope.addPath = function () {
$scope.logs.push($scope.inputData);
$scope.inputData = null;
};
}
推荐阅读
- amazon-web-services - 使用 AWS-CDK (python) 将多个现有安全组添加到 EC2
- python - Discord.py Massdm
- kubernetes - 在 Kubeadm 1.13.12 上生成新的 conf 文件
- javascript - 如何在 jQuery 中使用 for 循环替换 div 中的文本
- javascript - 如何在 React 和新页面中链接 2 个组件仍然有一个导航栏
- r - 将一列与多列进行比较并在 r 中用 dplyr 覆盖值
- for-loop - 如果网站上不存在查询,如何继续循环
- javascript - 如何在 Javascript 中设置默认参数而不影响其他参数
- python - Discord.py 如何让机器人向特定频道发送消息
- pandas - Pandas 在相似日期(+-7 天)合并 DataFrame