AngularJS动态地向表单添加文本框元素

时间:2014-03-01 10:31:45

标签: javascript angularjs

如何使用AngularJs动态地向表单添加文本框元素。例如,在这种情况下,我有一个文本和文本框,我想通过使用AngularJs单击按钮来添加该对的另一个。

<div ng-app>
<div ng-controller="ClickToEditCtrl">
    <div ng-hide="editorEnabled" ng-click="editorEnabled=true">
        {{title}}
    </div>
    <div ng-show="editorEnabled">
        <input ng-model="title">
        <button href="#" ng-click="editorEnabled=false">Done editing</button>
    </div>
</div>
</div>
<div>
<input type="text" placeholder="Enter answer">
</div>

3 个答案:

答案 0 :(得分:9)

我自己实施了它。您可以在

  • 中使用ng-repeat动态添加元素
    <li ng-repeat="elemnt in questionelemnt">
    

    这是Demo:fiddle

  • 答案 1 :(得分:3)

    js file

    $scope.choices = [{id: 'choice1'}, {id: 'choice2'}, {id: 'choice3'}];
    
    $scope.addNewChoice = function() {
        var newItemNo = $scope.choices.length+1;
        $scope.choices.push({'id':'choice' +newItemNo});
    };
    
    $scope.showAddChoice = function(choice) {
       return choice.id === $scope.choices[$scope.choices.length-1].id;
    };
    

    html

    <div class="form-group" data-ng-repeat="choice in choices">
        <label for="choice" ng-show="showChoiceLabel(choice)">Choices</label>
            <button ng-show="showAddChoice(choice)" ng-click="addNewChoice()">Add another  choice</button>
        <input type="text" ng-model="choice.name" name="" placeholder="Enter a restaurant name">
    </div>
    

    答案 2 :(得分:2)

    &#13;
    &#13;
    nil > 5 = ??
    &#13;
    &#13;
    &#13;