2017-12-05 19 views
8

Tôi đang sử dụng Bộ định tuyến giao diện người dùng góc. Vui lòng tìm mã bên dưới.Không thể tải ui-view ngay cả khi trạng thái thay đổi

index.html

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"></script> 
    <script src="../../Scripts/AngularControllers/LoginHomeControllers/RouteMainController.js"></script> 
    <script src="../../Scripts/AngularControllers/LoginHomeControllers/LoginController.js"></script> 
</head> 
<body> 
    <div ui-view="mainview"></div> 
</body> 
</html> 

RouteMainController.js

var app = angular.module("appHome", ['ui.router']); 

app.config(function ($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise('/login'); 
    $stateProvider 
     .state('introduction', { 
      url: '/', 
      views: { 
       'mainview': 
       { 
        templateUrl: 'Login.html', 
        controller: 'ctrlLogin' 
       } 
      } 
     }) 
     .state('login', { 
      url: '/login', 
      views: { 
       'mainview': 
       { 
        templateUrl: 'Login.html', 
        controller: 'ctrlLogin' 
       } 
      } 
     }) 
     .state('home', { 
      url: '/home', 
      views: { 
       'mainview': 
       { 
        templateUrl: 'Home.html', 
       } 
      } 
     }) 
     .state('home.requestsummary', { 
      url: '/requestsummary', 
      views: { 
       'homedetailview': 
       { 
        templateUrl: 'Test1.html', 
       } 
      } 
     }) 
     .state('home.requestsummary.requestdetail', { 
      url: '/requestdetail', 
      views: { 
       'homedetailview': 
       { 
        template: "'<h3>Foo</h3>'" 
       } 
      } 
     }) 
}); 

login.html

<div> 
    <button id="btnAdd" type="submit" ng-click="Login()">Login</button> 
</div> 

LoginController.js

var myApp = angular.module('appHome'); 
myApp.controller("ctrlLogin", ['$scope', '$location', function ($scope, $location) { 
    $scope.Login = function() { 
     window.location.href = '#!home/requestsummary';  
    } 
}]); 

home.html

<!--Sample Code for displaying Menu Tab and corresponding link--> 
<li> 
    <a href="#">Employees <span class="caret"></span></a> 
    <ul class="dropdown-menu sub-menu"> 
     <li><a ui-sref=".employeeadd">Add Employee Record</a></li>  
    </ul> 
</li> 

<div ui-view="homedetailview"></div> 

Test1.html

<a ui-sref=".requestdetail">Hello World</a> 

Tôi có thể đăng nhập thành công và nhận được tab menu với "Hello Liên kết thế giới. Tôi muốn hiển thị Menu Tab luôn luôn và thay đổi văn bản bên dưới động.

Vấn đề của tôi là bất cứ khi nào tôi nhấp vào liên kết "Hello World", tôi mong đợi Foo xuất hiện thay cho liên kết "Hello World" nhưng nó không hoạt động. Thậm chí nó đang thay đổi trạng thái (url) nhưng nó không tải chế độ xem thực tế.

Hãy giúp tôi về điều này.

+0

Có vẻ như bạn muốn 'requestdetail' là anh chị em của' requestummary' thay vì trạng thái con cháu – shaunhusain

+0

Có thể có nhiều liên kết như "Hello World" trong trang requestummary và nó sẽ mở trang requestdetail dựa trên liên kết được nhấp. Nhưng bất cứ khi nào liên kết được nhấp thì nội dung requestdetail sẽ ghi đè nội dung requestummary. đường ra là gì? – user1843970

Trả lời

7

Có sự cố về cấu trúc với mã của bạn. Tiểu bang requestdetail là trạng thái con của home.requestsummary và do đó, yêu cầu phải có ui-view trong mẫu của trạng thái home.requestsummary, có thể liên kết mã HTML.

Theo mã của bạn, không có ui-view trong Test1.html. Vì vậy, chỉ để đảm bảo rằng không có vấn đề khác, bạn có thể thử thêm một ui-view trong Test1.html, và thay đổi cấu hình trạng thái như sau:

<!-- Test.html --> 
<a ui-sref=".requestdetail">Hello World</a> 
<div ui-view></div> 

// RouteMainController.js 
... 
.state('home.requestsummary.requestdetail', { 
    url: '/requestdetail', 
    template: "'<h3>Foo</h3>' 
}) 

Sau sự thay đổi này, chỉ cần kiểm tra xem tiêu đề Foo xuất hiện Theo quan điểm. Nếu nó xuất hiện, thì mọi thứ đều ổn với mã của bạn. Bây giờ, phần thứ hai là tải tiêu đề Foo tại cùng một vị trí nơi liên kết của Xin chào thế giới được hiển thị.

Để làm điều đó, bạn cần phải thay đổi mục tiêu chế độ xem, bởi vì homedetailview tồn tại trong Home.html, là trạng thái gốc là home.requestsummary. Vì vậy, bạn cần phải thay đổi cấu hình nhà nước cho home.requestsummary.requestdetail như sau:

// RouteMainController.js 
... 
.state('home.requestsummary.requestdetail', { 
    url: '/requestdetail', 
    views: { 
    '[email protected]': { 
     template: "'<h3>Foo</h3>' 
    } 
    } 
}) 

này hoàn toàn sẽ nhắm vào homedetailview ở bang home, và mẫu của bạn cho home.requestsummary.requestdetail sẽ được hiển thị ở đó.

+2

Yup đồng ý với điều này chỉ là làm việc trên một ví dụ để trả lời ở đây https://next.plnkr.co/edit/IxZW3MjMTgoAdmQy – shaunhusain

+0

@shaunhusain - Cảm ơn bạn đã thực hiện một plunker cho nó. Thực sự hữu ích! – 31piy

+2

Không có vấn đề gì về việc bao gồm tất cả mã trong câu hỏi tôi phải dành thời gian để sao chép và dán sau đó có thể xem vấn đề của bạn đang hoạt động, luôn dễ dàng hơn để giải quyết khi bạn có thể thấy nó xảy ra. – shaunhusain

Các vấn đề liên quan