0

I have a navigation dropdown and I want to hide some of the options in the dropdown based on a value from a factory. I am using ng-show on the options that I want to hide or show based on the value from factory.

When I set it to false directly without the factory it works and I don't see those options in the dropdown.

This is how I get the data from factory:

$scope.mstrClientLStatus=userDetailsFactory.getUserDetailsFromFactory().mstrClientLStatus; 
console.log($scope.mstrClientLStatus) 
//it is false but doesn't hide

When I set the above to false directly the options are hidden.

$scope.mstrClientLStatus= false //works and the options are hidden

I console.log the $scope.mstrClientLStatus in the next line and it is false but it still shows the options in the dropdown.

The html:

<li ng-show="mstrClientLStatus"> //this is what I want to hide

The entire factory code:

.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;
    }
};
Abhi
  • 1,512
  • 2
  • 22
  • 46

2 Answers2

2

My colleague suggested this and it worked although it is a workaround ..I think:

if(userDetailsFactory.getUserDetailsFromFactory().mstrClientLStatus=="true"){
    $scope.mstrClientLStatus= true;
}
Abhi
  • 1,512
  • 2
  • 22
  • 46
  • See [SO: How can I convert a string to boolean in JavaScript?](http://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript). – georgeawg Oct 07 '16 at 11:58
  • @georgeawg. i thought `ng-show` considers both `"false"` and `false` as `false` – Abhi Oct 07 '16 at 12:16
  • Only the empty string `""` is falsy. Both strings `"false"` and `"true"` are truthy. In fact any string with a length greater than zero is truthy. This is one to the quirks of JavaScript. For more information, see [MDN glossary - Truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) and [MSN glossary Falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). – georgeawg Oct 07 '16 at 18:01
0

This is pretty much weird, giving you are writing code in controller, maybe a problem of digest cycle, all you need to put your operation inside timeout block to get it register in digest cycle,

  $timeout(function() {
   $scope.mstrClientLStatus = userDetailsFactory.getUserDetailsFromFactory().mstrClientLStatus;
   $scope.$apply()
 })
Siddharth
  • 859
  • 8
  • 16