如何在angularjs中单击按钮时将文本框值添加到数组并添加所需的功能

时间:2018-07-11 11:25:08

标签: javascript html 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(' ');
};

1 个答案:

答案 0 :(得分:0)

您正在使用$parent,但不需要使用它。我为此创建了一个简单的示例。您可以在HERE

中查看示例

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;
    };
}