2017-03-02 50 views
3

這是我第一次使用ui-router,在處理路由和重新加載某些狀態時遇到困難。我希望任何人都可以幫助我。當訪問頁面時,ui-router在我的ui-views中顯示原始代碼

概述我的index.html

<html ng-app="resApp"> 
    <head></head> 
    <body ng-controller="mainCtrl" ng-cloak> 
    <div ng-show="showCustomerContent"> 
     <div id="page-wrapper"> 
       <div ui-view="header"></div> 
       <div ui-view="slider" ng-show="displaySlider"></div> 
       <div ui-view="content"></div> 
       <div ui-view="footer"></div> 
     </div> 
    </div> 

    <div ng-show="showAdminContent"> 
      <div id="page-wrapper"> 
       <div ui-view="aheader"></div> 
       <div ui-view="asidebar"></div> 
       <div id="page-content-wrapper"> 
       <div id="page-content"> 
        <div class="container-fluid"> 
         <div ui-view="acontent"></div> 
        </div> 
       </div> 
    </div> 
    <div ui-view="jsincludes"></div> 

    </body> 
</html> 

routing.js

'use strict' 
     resApp.config(['$stateProvider', '$urlRouterProvider','$httpProvider','$locationProvider',function ($stateProvider, $urlRouterProvider, $httpProvider,$locationProvider) { 
       $urlRouterProvider 
       .when('/','/Home') 
       .when('/login','/Home') 
       .otherwise('/Home'); 

       $stateProvider 
       .state("home",{ 
       url:'/Home', 
       views:{ 
        'jsincludes':{ 
         templateUrl:'views/includes/jsincludes.html' 
        }, 
        'header':{ 
         templateUrl:'views/customer_part/header.html', 
        controller:'headerCtrl' 
        }, 
        'slider':{ 
         templateUrl:'views/customer_part/slider.html' 
        }, 
        'footer':{ 
         templateUrl:'views/customer_part/footer.html' 
        } 
       } 

       }) 
       .state("home.aboutus",{ 
       url:"/AboutUs", 
       views:{ 
        '[email protected]':{ 
        templateUrl:'views/customer_views/aboutus.html', 
        controller:'aboutusCtrl' 
          } 
         } 
       }) 
       $httpProvider.useApplyAsync(true); 
     }]); 

controller.js

'use strict' 
resApp.controller('mainCtrl',['$scope','$location','UserData','$rootScope',function($scope,$location,UserData,$rootScope){ 
    $rootScope.displaySlider = true; 
    $scope.$on('$stateChangeSuccess',function(event, toState){ 
     var url = $location.absUrl().split('/'); 
     //console.log(url); 
     if(url.length > 6){ 
      $rootScope.displaySlider = false; 
     }else{ 
      $rootScope.displaySlider = true; 
     } 

    }); 

    UserData.getSessVar().then(function(msg){ 
     if(msg.data!="none"){ 
      $rootScope.uid = msg.data["uid"]; 
      $rootScope.fullname = msg.data["fullname"]; 
      $rootScope.role = msg.data["role"]; 
      $rootScope.Id = msg.data["Id"]; 
      if($rootScope.role == "customer"){ 
       $scope.showCustomerContent = true; 
      }else{ 
       $scope.showAdminContent = true; 
      } 
     }else{ 
      $rootScope.role = "none"; 
      $scope.showCustomerContent = true; 
     } 

    }); 


}]); 
resApp.controller('headerCtrl',['$scope','$rootScope','UserData','$location','$state','$stateParams',function($scope,$rootScope,UserData,$location,$state,$stateParams){ 
    $scope.hideLoginBtn = false; 
    if($rootScope.uid!=undefined){ 
     $scope.hideLoginBtn = true; 
    } 

    $scope.logout = function(){ 
     UserData.logoutUser($rootScope.Id).then(function(msg){ 
      if(msg.data=="success"){ 
       window.location.reload(); 
       //$location.path('/Home'); 
      } 
     }); 
    } 

    $scope.home = function(){ 
     $state.go('home', {}, { reload: true }); 
    } 

    $scope.aboutus = function(){ 
     $state.go('home.aboutus',{},{reload:true}); 
    } 
}]); 

resApp.controller('aheaderCtrl',['$scope','$rootScope','UserData','$location',function($scope,$rootScope,UserData,$location){ 
    $scope.logout = function(){ 
     UserData.logoutUser($rootScope.Id).then(function(msg){ 
      if(msg.data=="success"){ 
       window.location.reload(); 
       //$location.path('/Home'); 
      } 

     }); 
    } 
}]); 

在我header.html中我有這樣的代碼

<li> 
    <a href="" title="Home" ng-click="home()"> 
     Home 
    </a> 
</li> 

Image when I click Home

每當我點擊Homeindex.html顯示的是我頭和滑塊的另一個副本。正如您在圖像中看到的那樣,{{fullname}}示波器已顯示,另一個ui-views。我試着做ui-sref="home" ui-sref-opts="{reload: true, notify: true}"並把它放在我的錨標記中,但結果是一樣的。我需要重新加載頁面,因爲我的滑塊不顯示,如果我只是做ui-sref="home"。我也嘗試使用ng-cloak,但每當我點擊錨標記時,它也會以angularjs顯示我的原始代碼。顯示的原始代碼在整個頁面呈現時消失。誰能幫我這個?

更新 這個錯誤只出現在Mozilla Firefox中。我的路由在Chrome中是非常好的

+0

哪個角度和UI路由器的版本,您使用的是做plunker?我想在一些在線代碼編輯器中重新創建這個問題。 –

+0

我使用了它的最新版本sir – pryxen

+0

你可以做一個你的代碼的Plunker? – Mistalis

回答

1

有div標籤上下面的代碼關閉問題

<div ng-show="showAdminContent"> 
      <div id="page-wrapper"> 
       <div ui-view="aheader"></div> 
       <div ui-view="asidebar"></div> 
       <div id="page-content-wrapper"> 
       <div id="page-content"> 
        <div class="container-fluid"> 
         <div ui-view="acontent"></div> 
        </div> 
       </div> 
    </div> 

其更改如下

<div ng-show="showAdminContent"> 
      <div id="page-wrapper"> 
       <div ui-view="aheader"></div> 
       <div ui-view="asidebar"></div> 
       <div id="page-content-wrapper"> 
       <div id="page-content"> 
        <div class="container-fluid"> 
         <div ui-view="acontent"></div> 
        </div> 
       </div> 
     </div> 
</div> 

另外,我不知道爲什麼你是否將頁眉,頁腳和滑塊保持爲命名的ui-view,而使用簡單的ng-include可以實現相同的效果。僅使用路由進行重定向和超鏈接。

請參閱我的如何

https://plnkr.co/edit/ZatFwz83ODsPjy55eRc5?p=preview

+0

哦,我從來沒有檢查過它。先生,謝謝你。 – pryxen

+0

歡迎您:) – Kalyan

1

理想情況下,您應該有另一個html文件,例如: - main.html,並在main.html中指定ui-view。爲前: -

<div id="wrapper"> 
<div ui-view="header"></div> 
<div ui-view></div> 
<div ui-view="footer"></div> 

+0

我會試試這個先生 – pryxen

+0

你能告訴我該怎麼做先生嗎? – pryxen

相關問題