首页 > 解决方案 > 如何在单击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(' ');
};

标签: javascripthtmlangularjs

解决方案


你正在使用$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;
    };
}

推荐阅读