简体   繁体   English

在Angular JS中ng提交后如何隐藏表格

[英]How to hide form after ng-submit in Angular JS

I have a form on my index that takes the input of a name. 我的索引上有一个输入名称的表格。 Upon submission of the name using ng-submit, I want to hide the form, then show another div which isn't currently hidden below. 使用ng-submit提交名称后,我想隐藏表单,然后在下面显示当前未隐藏的另一个div。 I have tried adding in a button to the form to set an ng-click and to show on click but cannot get it working (I took the button out) and just have the submit input. 我尝试在表单中添加一个按钮以设置ng-click并在单击时显示,但是无法使其正常工作(我将按钮取了出来),仅提交了输入。

HTML 的HTML

<form name="add-name" id="add-name" ng-submit="mainCtrl.addName()">
    <input type="text" class="enter-name" placeholder="Enter your name here..." ng-model="mainCtrl.newName.name">
</form>

<!--- this part should be hidden, but should show when form is submitted --->
<div class="steptwo">
    <h4 class="steptwo-text">{{ mainCtrl.newName.name }}</h4>
</div>

Controller 控制者

app.controller('mainController', function($scope) {

    this.newName = { name: '' };

    this.names = this.names || [
        { name: ' ' },
    ];

    this.addName = function() {

        this.names.push({
            name: this.newName.name
        });

        this.newName.name = null;
    };
    return this;
});

Any explanation or help would be greatly appreciated. 任何解释或帮助将不胜感激。

You can use ng-if or ng-show/hide directive for showing and hiding div depends on expression... 您可以使用ng-ifng-show/hide指令显示和隐藏div取决于表达式...

so first add this directive to part of html you want to show and hide and give them same expression... 因此,首先将此指令添加到要显示和隐藏的html部分中,并为其赋予相同的表达式...

<form name="add-name" id="add-name" ng-submit="mainCtrl.addName()" ng-hide="mainCtrl.hideForm">
    <input type="text" class="enter-name" placeholder="Enter your name here..." ng-model="mainCtrl.newName.name">
</form>

<!--- this part should be hidden, but should show when form is submitted --->
<div class="steptwo" ng-show="mainCtrl.hideForm">
    <h4 class="steptwo-text">{{ mainCtrl.newName.name }}</h4>
</div>

then in your controller just set hideForm variable true so form will be hidden and second step will be shown... 然后在您的控制器中只需将hideForm变量设置为true即可隐藏表单并显示第二步...

this.addName = function() {

    this.names.push({
        name: this.newName.name
    });

    this.newName.name = null;

    // hide form
    this.hideForm = true;

};

HTML: HTML:

 <form name="add-name" id="add-name" ng-submit="mainCtrl.addName()" ng-if="hide">
    <input type="text" class="enter-name" placeholder="Enter your name here..." ng-model="mainCtrl.newName.name">
</form>

<!--- this part should be hidden, but should show when form is submitted --->
<div class="steptwo" ng-if="!hide">
    <h4 class="steptwo-text">{{ mainCtrl.newName.name }}</h4>
</div>

Controller: 控制器:

app.controller('mainController', function($scope) {
$scope.hide = false;
this.newName = { name: '' };

this.names = this.names || [
    { name: ' ' },
];

this.addName = function() {

    this.names.push({
        name: this.newName.name
    });

    this.newName.name = null;
};
$scope.hide = true;
return this;

}); });

您可以在<form>标签上使用ng-ifng-showng-hide并从控制器控制值。

HTML File: HTML档案:

<form name="add-name" id="add-name" ng-submit="mainCtrl.addName()" ng-hide="hide">
    <input type="text" class="enter-name" placeholder="Enter your name here..." ng-model="mainCtrl.newName.name">
    <input type="button" ng-click="{{hide=true}}">
</form>

<!--- this part should be hidden, but should show when form is submitted --->
<div class="steptwo" ng-show="hide">
    <h4 class="steptwo-text">{{ mainCtrl.newName.name }}</h4>
</div>

JavaScript file: JavaScript文件:

app.controller('mainController', function($scope) {
    $scope.hide = false;
    this.newName = { name: '' };

    this.names = this.names || [
        { name: ' ' },
    ];

    this.addName = function() {

        this.names.push({
            name: this.newName.name
        });

        this.newName.name = null;
    };
    return this;
});

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

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