我的索引上有一个表单,它接受一个名称的输入。在使用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;
});
非常感谢任何解释或帮助。
答案 0 :(得分:1)
您可以使用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;
};
答案 1 :(得分:0)
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;
});
答案 2 :(得分:0)
您可以在ng-if
标记上使用ng-show
或ng-hide
或<form>
,并控制来自控制器的值。
答案 3 :(得分:0)
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;
});