单击按钮并显示新的html元素

时间:2017-04-03 12:04:47

标签: html angularjs

     <div>
        ToDo<br>
        <input ng-show="add" type="checkbox" >{{val}}
    </div>

    <div>
        <input type="text" ng-model="ToDo">
        <input type="button" ng-click="addToDo()" name="btn" placeholder="add todo here" ng-model="add" value="add" />
    </div>

和javascript:

  var app = angular.module("myapp",[]);
        app.controller("myctrl",function($scope,$log){
            $scope.addToDo = function(){
                $scope.val = $scope.ToDo;
            }
        });

如果我点击“添加&#39;按钮,复选框应与texxtbox的文本一起显示。但只显示文字,没有复选框。如何显示复选框?

1 个答案:

答案 0 :(得分:1)

您必须在控制器中调用该函数并将某个变量更新为true / false。根据值并使用ng-if指令,您可以切换HTML元素。

&#13;
&#13;
$scope.displayCheckBox = false;
$scope.val = "someRandoValue";

$scope.addToDo = function() {
  $scope.displayCheckBox = true;
};
&#13;
<div>
  ToDo<br>
  <span ng-if="displayCheckBox">
    <input type="checkbox" >{{val}}
  </span>
</div>
&#13;
&#13;
&#13;