2016-10-06 63 views
0

我有一个导航下拉菜单,我想根据工厂的值隐藏下拉菜单中的一些选项。我正在使用ng-show来选择要隐藏或显示基于工厂值的选项。设置false为范围变量在工厂设置时不起作用

当我将它设置为false而没有工厂时,它的工作原理和我在下拉菜单中看不到这些选项。

这是我从厂家得到的数据:

$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 

console.log下一行的$scope.mstrClientLStatus,这是假的,但它仍然显示在下拉菜单中的选项。

的HTML:

<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; 
    } 
}; 
+0

尝试'$范围$适用()''中'$ scope.mstrClientLStatus = userDetailsFactory.getUserDetailsFromFactory()mstrClientLStatus的底部; ' – itzMEonTV

+0

我得到这个错误'https://docs.angularjs.org/error/$rootScope/inprog?p0 = $ digest' – Abhi

+0

你可以发布你的工厂片段吗? – Siddharth

回答

2

我科尔疟疾建议这和它的工作,虽然这是一个解决办法..我想:。

if(userDetailsFactory.getUserDetailsFromFactory().mstrClientLStatus=="true"){ 
    $scope.mstrClientLStatus= true; 
} 
+0

请参阅[SO:如何将字符串转换为布尔值在JavaScript?](http://stackoverflow.com/questions/263965/how-can-i-convert-一个字符串到布尔在JavaScript的)。 – georgeawg

+0

@georgeawg。我认为'ng-show'认为'false'和'false'都是'false' – Abhi

+0

只有空字符串'“”'是虚假的。字符串'“false”和“true”都是真的。事实上,任何长度大于零的字符串都是真的。这是JavaScript的怪癖之一。有关更多信息,请参阅[MDN词汇表 - Truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)和[MSN词汇表Falsy](https://developer.mozilla.org/en -US /文档/词汇/ Falsy)。 – georgeawg

0

这是非常奇怪的,让你在控制器写代码,消化周期可能出了问题,所有你需要的把你的操作超时块内得到它在消化周期寄存器,

$timeout(function() { 
    $scope.mstrClientLStatus = userDetailsFactory.getUserDetailsFromFactory().mstrClientLStatus; 
    $scope.$apply() 
}) 
+0

我已经发布了一个可行的解决方案,但不是很对。你可以看看它 – Abhi

+0

完全好,10/10 – Siddharth