2016-10-06 99 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