2015-11-11 98 views
0

我想顯示/隱藏一個div根據配置設置返回的值返回{}。如果UserProfileLinkData設置爲1,我想向他們展示底部,如果它設置爲0,我想向他們展示頂部。如何顯示Div使用ng顯示從返回值

返回值在一個配置服務中,在該視圖的控制器中稱爲依賴項。我知道這是工作,因爲我可以從configurationService調用LinkToken。

return { 
    UserProfileLinkData: 1, 
    LinkToken: userLinkToken 
} 

以下是根據此值隱藏/顯示的視圖和's'。

<ul class="list-unstyled" ng-show="UserProfileLinkData === 0"> 
    <li><a style="cursor: pointer;" ng-if="!user.IsLinked" ng-click="openFindPrescriptionModal()">Find My Profile</a></li> 
    <li ng-show="false"><a style="cursor: pointer;">Add my Dependents</a></li> 
</ul> 



<ul class="list-unstyled" ng-show="UserProfileLinkData === 1"> 
    <li><a style="cursor: pointer;" ng-if="!user.IsLinked" ng-click="openFindPrescriptionModal()">Link My User Profile</a></li> 
    <li ng-show="false"><a style="cursor: pointer;">Add my Dependents</a></li> 
</ul> 

這裏是從控制器

angular.module('User.Controllers').controller('homeController', function ($scope, $rootScope, $modal, userService, configurationService, accountService,$location) { 

$rootScope.showNavbar = true; 
$scope.user = userService.GetUserInformation(); 
$scope.updatingAcct = true; 
$scope.loading = false; 
$scope.UserProfileLinkData = configurationService.UserProfileLinkData; 
... 
+1

看看這個Plunkr根據你的問題:HTTP://embed.plnkr。 co/hqhfM7JIvabou2RW4Sos /預覽。有什麼我失蹤,因爲它看起來好像工作正常? – austinthedeveloper

+0

在你的視圖中使用「==」而不是「===」;) –

+0

@austinthedeveloper謝謝你的歡樂。我的控制器中有語法錯誤。完美的作品。 Boom –

回答

1

改變這一點:

ng-show="UserProfileLinkData === 0" 
ng-show="UserProfileLinkData === 1" 

此:

ng-show="UserProfileLinkData == 0" 
ng-show="UserProfileLinkData == 1" 

這對我的工作;)

0

確保您對這一觀點具有相同名稱的分配UserProfileLinkData服務變量控制器內部的變量代碼。你在做這個嗎?你能顯示你的控制器的代碼和注入的狀態服務嗎?

+0

我用相關的控制器代碼更新了我的問題。 Thansk –

-1

你可以嘗試使用你想在ng-show中訪問的變量的「$ scope」屬性。嘗試使用 -

return { 
    $scope.UserProfileLinkData: 1, 
    LinkToken: userLinkToken 
} 

之後,您可以使用相同的HTML代碼而不做任何更改。試着讓我看看這是否有效。