简体   繁体   English

使用Angular JS隐藏表单

[英]Hide form on submit with Angular JS

Trying to wrap my head around some Angular items and working thru a tutorial to edit and learn. 试图围绕一些Angular项目,并通过教程编辑和学习。

Clicking the below button shows the below form. 单击下面的按钮会显示以下表单。 How do I reverse this once the form is submitted? 提交表单后如何撤消此操作? Meaning hiding the form on submit until the button is clicked once more. 意味着将表单隐藏在提交之前,直到再次单击该按钮。

<button ng-click="addNewClicked=!addNewClicked;" class="btn btn-sm btn-primary">
    <i class="fa fa-plus"></i>Add Task
</button>

Basically, the form appears, I enter something and submit, but would like the form to dissapear upon submit? 基本上,表格出现了,我输入内容并提交,但是希望表单在提交时消失? Thinking something to do with ng-hide, but can I do this using only Angular? 想与ng-hide有关,但我可以只使用Angular吗? Or do I need to do something with javascript/css? 或者我需要用javascript / css做些什么吗?

<div id="addForm" class="margin-full-5">
<form ng-init="addNewClicked=false; " ng-if="addNewClicked" id="newTaskForm" class="add-task">
    <div class="form-actions">
        <div class="input-group">
            <input type="text" class="form-control" name="comment" ng-model="taskInput" placeholder="Add New Task" ng-focus="addNewClicked">
            <div class="input-group-btn">
                <button class="btn btn-default" type="submit" ng-click="addTask(taskInput)">
                    <i class="fa fa-plus"></i>&nbsp;Add Task
                </button>
            </div>
        </div>
    </div>
</form>

You can also achieve this using a combination of Angular form's attribute $submitted , ng-hide and ng-submit 您还可以使用Angular表单的属性$submittedng-hideng-submit的组合来实现此目的

<form name="myForm" ng-hide="myForm.$submitted" ng-submit="submit()">
    <button>Submit</button>
</form>

Read about it here: https://docs.angularjs.org/api/ng/type/form.FormController 在这里阅读: https//docs.angularjs.org/api/ng/type/form.FormController

Somewhere in your view. 在你看来的某个地方。

<button ng-click="showTheForm = !showTheForm">Add a Task</button>

<form ng-show="showTheForm" ng-submit="processForm()">
    <button>Submit</button>
    <button type="button" ng-click="showTheForm = false">Cancel</button>
</form>

Somewhere in your controller 在控制器的某个地方

$scope.processForm = function() {
    // execute something
    $scope.showTheForm = false;
}

Your form is displaying IF the addNewClicked value evaluates to true, which occurs when you click the add task button. 如果 addNewClicked值的计算结果为true,则会显示表单,单击“添加任务”按钮时会出现该表单。 If you want the form to disappear on submit, you just need to make the onClick to that button change your addNewClicked to false. 如果您希望表单在提交时消失,您只需要将onClick设置为该按钮,将addNewClicked更改为false。

AngularJS Docs for Ng-If AngularJS Docs for Ng-If

You can do that by using ng-show/ng-hide as per example below : 您可以使用ng-show / ng-hide执行此操作,如下例所示:

<form ng-init="addNewClicked=false; " ng-if="addNewClicked" ng-hide="hideform" id="newTaskForm" class="add-task">

and modify the submit method to make the hideform = true; 并修改submit方法以使hideform = true;

$scope.addTask = function(input){
... your things
$scope.hideform = true; 
}

You can also do the same using jQuery : 你也可以使用jQuery做同样的事情:

$("#newTaskForm").hide(); 

This should do the trick: 这应该做的伎俩:

$scope.addTask = function(taskInput) {
    ...
    $scope.addNewClicked = false; 
}

You could use ng-show as you can see in this jsfiddle 你可以在这个jsfiddle中看到ng-show

This will show and hide the div element based on clicking the button. 这将基于单击按钮显示和隐藏div元素。 When the button is clicked it will toggle the boolean, hence acting as an on/off switch for ng-show 单击该按钮时,它将切换布尔值,因此充当ng-show的开/关开关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM