ng-show ng-hide基于$ scope变量无法正常工作

时间:2015-12-03 18:10:26

标签: angularjs

我想基于$ rootScope变量显示和隐藏。我在范围内采用变量并在所有函数中将其赋值为true,并且所有函数都在同一个控制器中,基于I ng-show ..但它在切换时不会隐藏其他函数。

Controller.js:

$rootScope.pcity = true;
  if(geolocation.subLocality1 != null){
    $rootScope.subLocality=true;

    var getCityPopularUsers = function() {              
      var pageNumber = 0;
      $scope.pageSize = 6;
      var data = "pageNumber=" + pageNumber + "&pageSize=" 
                  + $scope.pageSize + "&city=" + $routeParams.searchText;
      homeService.getPopularUsers(data, function(users) {
        $rootScope.cPopUserss = users;
      });
    };

    getCityPopularUsers();

    var getSubLocalityPopularTags = function() {
      var pageNumber = 0;
      $scope.pageSize = 6;
      var data = "pageNumber=" + pageNumber + "&pageSize=" 
                  + $scope.pageSize + "&city=" + $routeParams.searchText;
      homeService.getSubLocalityPopularTags(data, function(tags) {
        $rootScope.cPopsubTags = tags;
      });
    };

    getSubLocalityPopularTags();
  }

  if(geolocation.city != null) {
    $rootScope.city=true;
    var getPopularByCityUsers = function() {
      var pageNumber = 0;
      $scope.pageSize = 6;
      var data = "pageNumber=" + pageNumber + "&pageSize=" 
                  + $scope.pageSize + "&city="+ $routeParams.searchText;
      homeService.getPopularByCityUsers(data, function(users) {
        $rootScope.cPopUsers = users;
      });
    };

    getPopularByCityUsers();

    var getCityPopularTags = function() {
      var pageNumber = 0;
      $scope.pageSize = 6;
      var data = "pageNumber=" + pageNumber + "&pageSize=" 
                  + $scope.pageSize + "&city="+ $routeParams.searchText;
      homeService.getPopularTagsByCity(data, function(tags) {
        $rootScope.cPopTags = tags;
      });
    };

    getCityPopularTags();
  }

  if(geolocation.state != null) {
    $rootScope.state=true ;
    var getPopularByStateUsers = function() {
    var pageNumber = 0;
    $scope.pageSize = 6;
    var data = "pageNumber=" + pageNumber + "&pageSize=" 
                + $scope.pageSize + "&city=" + $routeParams.searchText;
    homeService.getPopularByStateUsers(data, function(users) {
      $rootScope.cPopStateUsers = users;
    });
  };

  getPopularByStateUsers();

  var getStatePopularTags = function() {
    var pageNumber = 0;
    $scope.pageSize = 6;
    var data = "pageNumber=" + pageNumber + "&pageSize=" 
                + $scope.pageSize + "&city="+ $routeParams.searchText;
    homeService.getStatePopularTags(data, function(tags) {
      $rootScope.cPopStateTags = tags;
    });
  };

  getStatePopularTags();
}

这是我的index.jsp:

<div class="popular-account-list list-home-aside" ng-show="pcity == true && subLocality == true  ">
  <ul class="nav">
    <li class="active icon bb-users">Popular Account</li>
    <li ng-repeat="user in cPopUserss | limitTo : 4">
      <figure class="clearfix" ng-show="loggedInUser.id!=user.id">
        <div class="home-acc-imgsize" ng-if="user.photo != null">
          <a href="#!/profile/user/{{user.id}}">
            <img ng-src="settings/dp?username={{user.username}}&photoId={{user.photo.id}}&fileName={{user.photo.fileName}}" 
                 alt="profile-img" class="img-responsive" style="height:100%;width:100%">
          </a>
        </div>
        <div class="home-acc-imgsize" ng-if="user.photo == null">
          <a href="#!/profile/user/{{user.id}}">
             <img src="images/userimage.jpg" alt="profile-img" class="img-responsive">
          </a>
        </div>

        <figcaption>
          <p ng-show="user.domainName == null">
            <a href="#!/profile/user/{{user.id}}">{{user.firstName}} {{user.lastName}}</a>
          </p>
          <p ng-show="user.domainName != null">
            <a href="#!/profile/user/{{user.id}}">{{user.username}}</a>
          </p>
          <p><span>{{user.designation}}</span></p>

          <button class="btn btn-following  btn-xs" role="button"
                  ng-show="isFollowed == false || isFollowed == 'false' && user.id != loggedInUser.id" 
                  ng-click="follow(user.id)">Follow</button>
          <button class="btn btn-following  btn-xs" role="button" ng-show="isFollowed == 'true'" 
                  ng-click="removeFollow(user.id)">Following</button>

        </figcaption>
      </figure>
    </li>

    <li><a href="#!/profile/allAccounts">More Accounts <span class="caret"></span></a></li>
  </ul>
</div><!-- eo popular-account-list-list -->

<div class="popular-account-list list-home-aside" ng-show="pcity == true && city == true">
  <ul class="nav">
    <li class="active icon bb-users" >Popular Account</li>
    <li ng-repeat="user in cPopUsers | limitTo : 4">
      <figure class="clearfix" ng-show="loggedInUser.id!=user.id">
        <div class="home-acc-imgsize" ng-if="user.photo != null">
          <a href="#!/profile/user/{{user.id}}">
            <img ng-src="settings/dp?username={{user.username}}&photoId={{user.photo.id}}&fileName={{user.photo.fileName}}" 
                 alt="profile-img" class="img-responsive" style="height:100%;width:100%">
          </a>
        </div>
        <div class="home-acc-imgsize" ng-if="user.photo == null">
          <a href="#!/profile/user/{{user.id}}">
            <img src="images/userimage.jpg" alt="profile-img" class="img-responsive">
          </a>
        </div>

        <figcaption>
          <p ng-show="user.domainName == null">
            <a href="#!/profile/user/{{user.id}}">{{user.firstName}} {{user.lastName}}
            </a>
          </p>
          <p ng-show="user.domainName != null">
            <a href="#!/profile/user/{{user.id}}">{{user.username}}</a>
          </p>
          <p><span>{{user.designation}}</span></p>

          <button class="btn btn-following  btn-xs" role="button"
                  ng-show="isFollowed == false || isFollowed == 'false' && user.id != loggedInUser.id" 
                  ng-click="follow(user.id)">Follow</button>
          <button class="btn btn-following  btn-xs" role="button" ng-show="isFollowed == 'true'" 
                  ng-click="removeFollow(user.id)">Following</button>

        </figcaption>
      </figure>
    </li>

    <li><a href="#!/profile/allAccounts">More Accounts <span class="caret"></span></a>
    </li>
  </ul>
</div><!-- eo popular-account-list-list -->

<div class="popular-account-list list-home-aside" ng-show="pcity == true && state == true ">
  <ul class="nav">
    <li class="active icon bb-users">Popular Account</li>
    <li ng-repeat="user in cPopStateUsers | limitTo : 4">
      <figure class="clearfix" ng-show="loggedInUser.id!=user.id">
        <div class="home-acc-imgsize" ng-if="user.photo != null">
          <a href="#!/profile/user/{{user.id}}">
            <img ng-src="settings/dp?username={{user.username}}&photoId={{user.photo.id}}&fileName={{user.photo.fileName}}" 
                 alt="profile-img" class="img-responsive" style="height:100%;width:100%">
          </a>
        </div>
        <div class="home-acc-imgsize" ng-if="user.photo == null">
          <a href="#!/profile/user/{{user.id}}">
            <img src="images/userimage.jpg" alt="profile-img" class="img-responsive">
          </a>
        </div>

        <figcaption>
          <p ng-show="user.domainName == null">
            <a href="#!/profile/user/{{user.id}}">{{user.firstName}} {{user.lastName}}
            </a>
          </p>
          <p ng-show="user.domainName != null">
            <a href="#!/profile/user/{{user.id}}">{{user.username}}</a>
          </p>
          <p><span>{{user.designation}}</span></p>

          <button class="btn btn-following  btn-xs" role="button"
                  ng-show="isFollowed == false || isFollowed == 'false' && user.id != loggedInUser.id" 
                  ng-click="follow(user.id)">Follow</button>
          <button class="btn btn-following  btn-xs" role="button" ng-show="isFollowed == 'true'" 
                  ng-click="removeFollow(user.id)">Following</button>

        </figcaption>
      </figure>
    </li>

    <li><a href="#!/profile/allAccounts">More Accounts <span class="caret"></span></a>
    </li>
  </ul>
</div><!-- eo popular-account-list-list -->

在控制器中,如果我使用$ scope而不是rootScope,则没有任何作用,所以我出错了。请帮助

0 个答案:

没有答案