[英]How to hide form after ng-submit in Angular JS
我的索引上有一个输入名称的表格。 使用ng-submit提交名称后,我想隐藏表单,然后在下面显示当前未隐藏的另一个div。 我尝试在表单中添加一个按钮以设置ng-click并在单击时显示,但是无法使其正常工作(我将按钮取了出来),仅提交了输入。
的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>
控制者
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;
});
任何解释或帮助将不胜感激。
您可以使用ng-if
或ng-show/hide
指令显示和隐藏div取决于表达式...
因此,首先将此指令添加到要显示和隐藏的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>
然后在您的控制器中只需将hideForm变量设置为true即可隐藏表单并显示第二步...
this.addName = function() {
this.names.push({
name: this.newName.name
});
this.newName.name = null;
// hide form
this.hideForm = true;
};
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>
控制器:
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-if
或ng-show
或ng-hide
并从控制器控制值。
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文件:
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.