我有一个用于回复消息的表单,我只想在isReplyFormOpen
为真时才显示,每次单击回复按钮时,我都想切换是否显示表单。我怎样才能做到这一点?
6 回答
您只需要在 ng-click 事件上切换“isReplyFormOpen”的值
<a ng-click="isReplyFormOpen = !isReplyFormOpen">Reply</a>
<div ng-show="isReplyFormOpen" id="replyForm">
</div>
基本上,我通过在单击它时NOT -ingisReplyFormOpen
值来解决它:
<a ng-click="isReplyFormOpen = !isReplyFormOpen">Reply</a>
<div ng-init="isReplyFormOpen = false" ng-show="isReplyFormOpen" id="replyForm">
<!-- Form -->
</div>
如果基于点击这里是:
ng-click="orderReverse = orderReverse ? false : true"
值得注意的是,如果您在控制器 A 中有一个按钮,并且要在控制器 B 中显示该元素,则可能需要使用点符号来跨控制器访问范围变量。
例如,这将不起作用:
<div ng-controller="ControllerA">
<a ng-click="isReplyFormOpen = !isReplyFormOpen">Reply</a>
<div ng-controller="ControllerB">
<div ng-show="isReplyFormOpen" id="replyForm">
</div>
</div>
</div>
要解决这个问题,请创建一个全局变量(即在控制器 A 或您的主控制器中):
.controller('ControllerA', function ($scope) {
$scope.global = {};
}
然后为您的点击添加一个“全局”前缀并显示变量:
<div ng-controller="ControllerA">
<a ng-click="global.isReplyFormOpen = !global.isReplyFormOpen">Reply</a>
<div ng-controller="ControllerB">
<div ng-show="global.isReplyFormOpen" id="replyForm">
</div>
</div>
</div>
有关更多详细信息,请查看 Angular-UI 文档中的嵌套状态和嵌套视图、观看视频或阅读理解范围。
如果您有多个带有子菜单的菜单,那么您可以使用以下解决方案。
HTML
<ul class="sidebar-menu" id="nav-accordion">
<li class="sub-menu">
<a href="" ng-click="hasSubMenu('dashboard')">
<i class="fa fa-book"></i>
<span>Dashboard</span>
<i class="fa fa-angle-right pull-right"></i>
</a>
<ul class="sub" ng-show="showDash">
<li><a ng-class="{ active: isActive('/dashboard/loan')}" href="#/dashboard/loan">Loan</a></li>
<li><a ng-class="{ active: isActive('/dashboard/recovery')}" href="#/dashboard/recovery">Recovery</a></li>
</ul>
</li>
<li class="sub-menu">
<a href="" ng-click="hasSubMenu('customerCare')">
<i class="fa fa-book"></i>
<span>Customer Care</span>
<i class="fa fa-angle-right pull-right"></i>
</a>
<ul class="sub" ng-show="showCC">
<li><a ng-class="{ active: isActive('/customerCare/eligibility')}" href="#/CC/eligibility">Eligibility</a></li>
<li><a ng-class="{ active: isActive('/customerCare/transaction')}" href="#/CC/transaction">Transaction</a></li>
</ul>
</li>
</ul>
我首先调用了两个函数,即 ng-click = hasSubMenu('dashboard')。此功能将用于切换菜单,并在下面的代码中进行了说明。ng-class="{ active: isActive('/customerCare/transaction')} 它将向当前菜单项添加一个活动类。
现在我在我的应用程序中定义了一些功能:
首先,添加一个用于声明变量和函数的依赖项 $rootScope。要了解有关 $rootScope 的更多信息,请参阅链接:https ://docs.angularjs.org/api/ng/service/ $rootScope
这是我的应用程序文件:
$rootScope.isActive = function (viewLocation) {
return viewLocation === $location.path();
};
上述函数用于将活动类添加到当前菜单项。
$rootScope.showDash = false;
$rootScope.showCC = false;
var location = $location.url().split('/');
if(location[1] == 'customerCare'){
$rootScope.showCC = true;
}
else if(location[1]=='dashboard'){
$rootScope.showDash = true;
}
$rootScope.hasSubMenu = function(menuType){
if(menuType=='dashboard'){
$rootScope.showCC = false;
$rootScope.showDash = $rootScope.showDash === false ? true: false;
}
else if(menuType=='customerCare'){
$rootScope.showDash = false;
$rootScope.showCC = $rootScope.showCC === false ? true: false;
}
}
默认情况下,$rootScope.showDash 和 $rootScope.showCC 设置为 false。最初加载页面时,它会将菜单设置为关闭。如果您有两个以上的子菜单,请相应添加。
hasSubMenu() 函数将用于在菜单之间切换。我添加了一个小条件
if(location[1] == 'customerCare'){
$rootScope.showCC = true;
}
else if(location[1]=='dashboard'){
$rootScope.showDash = true;
}
根据所选菜单项重新加载页面后,它将保持子菜单打开。
我已将我的页面定义为:
$routeProvider
.when('/dasboard/loan', {
controller: 'LoanController',
templateUrl: './views/loan/view.html',
controllerAs: 'vm'
})
仅当您有一个没有子菜单的菜单时,才可以使用 isActive() 函数。您可以根据需要修改代码。希望这会有所帮助。祝你有美好的一天 :)
这是使用 ngclick 和 ng-if 指令的示例。
注意:ng-if 从 DOM 中删除元素,但 ng-hide 只是隐藏元素的显示。
<!-- <input type="checkbox" ng-model="hideShow" ng-init="hideShow = false"></input> -->
<input type = "button" value = "Add Book"ng-click="hideShow=(hideShow ? false : true)"> </input>
<div ng-app = "mainApp" ng-controller = "bookController" ng-if="hideShow">
Enter book name: <input type = "text" ng-model = "book.name"><br>
Enter book category: <input type = "text" ng-model = "book.category"><br>
Enter book price: <input type = "text" ng-model = "book.price"><br>
Enter book author: <input type = "text" ng-model = "book.author"><br>
You are entering book: {{book.bookDetails()}}
</div>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('bookController', function($scope) {
$scope.book = {
name: "",
category: "",
price:"",
author: "",
bookDetails: function() {
var bookObject;
bookObject = $scope.book;
return "Book name: " + bookObject.name + '\n' + "Book category: " + bookObject.category + " \n" + "Book price: " + bookObject.price + " \n" + "Book Author: " + bookObject.author;
}
};
});
</script>