簡體   English   中英

從工廠設置為范圍變量設置false無效

[英]setting false to a scope variable doesn't work when set from factory

我有一個導航下拉菜單,我想根據工廠的值隱藏下拉菜單中的某些選項。 我在要根據出廠值隱藏或顯示的選項上使用ng-show

當我在沒有工廠的情況下直接將其設置為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 ,它為false,但仍在下拉列表中顯示選項。

的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;
    }
};

我的同事提出了這一建議,盡管它是一種解決方法,但它仍然有效。.我認為:

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

這簡直太奇怪了,給您在控制器中編寫代碼,也許是摘要周期的問題,您需要將操作放在超時塊中以使其在摘要周期中進行注冊,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM