我有一个导航下拉菜单,我想隐藏下拉菜单中基于工厂值的一些选项。我使用ng-show
的选项,我想隐藏或显示基于从工厂的值。
当我将它直接设置为false而不使用factory时,它会工作,并且我不会在下拉菜单中看到这些选项。
这是我从工厂获取数据的方式:
$scope.mstrClientLStatus=userDetailsFactory.getUserDetailsFromFactory().mstrClientLStatus;
console.log($scope.mstrClientLStatus)
//it is false but doesn't hide
当我将上述设置为直接false
时,选项被隐藏。
$scope.mstrClientLStatus= false //works and the options are hidden
I console.log
和$scope.mstrClientLStatus
在下一行,它是假的,但它仍然显示下拉菜单中的选项。
<li ng-show="mstrClientLStatus"> //this is what I want to hide
整个工厂代码:
.factory('userDetailsFactory',function(){
var user = {};
return {
setUserDetailsInFactory : function(val,$cookies,favoriteCookie,put,get){
user.useridFromSession = val[0].UserId;
user.usernameFromSession = val[0].UserName;
user.userroleFromSession = val[0].Role;
user.clientidFromSession = val[0].ClientId;
user.ip = "http://192.168.2.100:5000";
user.mstrDashBoardSession = val[0].DashBoard;
user.mstrClientLSession = val[0].ClientLogin;
user.ClientRegistrationSession = val[0].ClientRegistration;
user.mstrBustypeSession = val[0].mstrBustype;
user.mstrBusCategorySession = val[0].mstrBusCategory;
user.mstrSeatLayoutSession = val[0].mstrSeatLayout;
user.mstrDesignationSession = val[0].mstrDesignation;
user.mstrBusSession = val[0].mstrBus;
user.mstrRouteSession = val[0].mstrRoute;
user.mstrBranchSession = val[0].mstrBranch;
user.mstrDeviceSession = val[0].mstrDevice;
user.mstrServiceSession = val[0].mstrService;
user.mstrRouteFareSession = val[0].mstrRouteFare;
user.mstrNewUserSession = val[0].mstrNewUser;
user.mstrPDAServiceAssignSession = val[0].mstrPDAServiceAssign;
user.mstrUserRightSession = val[0].mstrUserRight;
user.rptTripSheetSession = val[0].rptTripSheet;
user.rptTripSummarySession = val[0].rptTripSummary;
user.rptCargoSummaryFromSession = val[0].rptCargoSummary;
user.rptBusMonthSession = val[0].rptBusMonth;
user.rptDeviceSession = val[0].rptDevice;
//console.log(user.useridFromSession);
//console.log(user.usernameFromSession);
//console.log(user.userroleFromSession);
},getUserDetailsFromFactory : function(){
return user;
}
};
我的同事建议这样做,虽然它是一个变通方法。我认为:
if(userDetailsFactory.getUserDetailsFromFactory().mstrClientLStatus=="true"){
$scope.mstrClientLStatus= true;
}
这很奇怪,因为你在控制器中编写代码,可能是摘要循环的问题,你只需要把你的操作放在超时块中让它在摘要循环中注册,
$timeout(function() {
$scope.mstrClientLStatus = userDetailsFactory.getUserDetailsFromFactory().mstrClientLStatus;
$scope.$apply()
})