2015-01-21 73 views
0

我僅具有AngularJS的基本知識。我創建了一個帶有多個html頁面的AngularJS應用程序。這是我的主要觀點html.Im很抱歉,我的問題是如此之久。但這是更好的方式來問我的問題。如何使用角度正確地路由多個html頁面

Main.cshtml

<html ng-app="myapp"> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Main</title> 
    <!-- Main CDN Links--> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css"> 
    <link rel="stylesheet" href="http://ajax.aspnetcdn.com/ajax/bootstrap/3.3.1/css/bootstrap-theme.css"> 
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"> 
    <!-- end--> 
    <link href="~/Content/Css/sidebar.css" rel="stylesheet" /> 
    <style> 
     * { 
      border-radius: 0 !important; 
     } 
    </style> 
</head> 
<body> 
<div class="nav-side-menu"> 
//some code 
</div> 
<div class="nav-top-menu nav-top"> 
//some code 
</div> 
<div class="contents" id="subContents" ui-view> 
//route my sub views to here 
</div> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script> 
    <script src="//angular-ui.github.io/ui-router/release/angular-ui-router.js"></script> 
    <script src="~/Scripts/smart-table.debug.js"></script> 
<script src="~/Scripts/MainView.js"></script> 
</body> 
</html> 

MainView.js

var myapp = angular.module('myapp', ["ui.router"]); 
myapp.config(function ($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise("/route1") 
    $stateProvider 
     .state('sideMenuCustomerDivition', { 
      templateUrl: "Main/Customer" 
     }) 
     .state('sideMenuDashboardDivition', { 
      templateUrl: "Main/Staff" 
     }) 
}) 

我有我的路線另一個HTML視圖到main.cshtml.This是,鑑於HTML

Customer.cshtml

<html> 
<body ng-app="customerTableApp"> 
    <div ng-controller="safeCtrl"> 

     <button type="button" ng-click="addRandomItem(row)" class="btn btn-sm btn-success"> 
      <i class="glyphicon glyphicon-plus"> 
      </i> Add random item 
     </button> 

     <table st-table="displayedCollection" st-safe-src="rowCollection" class="table table-striped"> 
      <thead> 
       <tr> 
        <th st-sort="firstName">first name</th> 
        <th st-sort="lastName">last name</th> 
        <th st-sort="birthDate">birth date</th> 
        <th st-sort="balance">balance</th> 
       </tr> 
       <tr> 
        <th colspan="5"><input st-search="" class="form-control" placeholder="global search ..." type="text" /></th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr ng-repeat="row in displayedCollection"> 
        <td>{{row.firstName}}</td> 
        <td>{{row.lastName}}</td> 
        <td>{{row.birthDate}}</td> 
        <td>{{row.balance}}</td> 
        <td> 
         <button type="button" ng-click="removeItem(row)" class="btn btn-sm btn-danger"> 
          <i class="glyphicon glyphicon-remove-circle"> 
          </i> 
         </button> 
        </td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
    <script src="~/Scripts/CustomerView.js"></script> 
</body> 
</html> 

CustomerView.js

var app = angular.module('customerTableApp', ['smart-table']) 
app.controller('safeCtrl', ['$scope', function ($scope) { 

    var firstnames = ['Laurent', 'Blandine', 'Olivier', 'Max']; 
    var lastnames = ['Renard', 'Faivre', 'Frere', 'Eponge']; 
    var dates = ['1987-05-21', '1987-04-25', '1955-08-27', '1966-06-06']; 
    var id = 1; 

    function generateRandomItem(id) { 

     var firstname = firstnames[Math.floor(Math.random() * 3)]; 
     var lastname = lastnames[Math.floor(Math.random() * 3)]; 
     var birthdate = dates[Math.floor(Math.random() * 3)]; 
     var balance = Math.floor(Math.random() * 2000); 

     return { 
      id: id, 
      firstName: firstname, 
      lastName: lastname, 
      birthDate: new Date(birthdate), 
      balance: balance 
     } 
    } 

    $scope.rowCollection = []; 

    for (id; id < 5; id++) { 
     $scope.rowCollection.push(generateRandomItem(id)); 
    } 
    $scope.displayedCollection = [].concat($scope.rowCollection); 
    $scope.addRandomItem = function addRandomItem() { 
     $scope.rowCollection.push(generateRandomItem(id)); 
     id++; 
    }; 
    $scope.removeItem = function removeItem(row) { 
     var index = $scope.rowCollection.indexOf(row); 
     if (index !== -1) { 
      $scope.rowCollection.splice(index, 1); 
     } 
    } 
}]); 

我知道問題出在NG-應用程序連到另一個NG-app.Anyone可以給我來查看已經排到了this.Template更好的解決方案。我的問題是我無法將值分配到第二個視圖。客戶視圖顯示在側面主視圖中。但分配的值不顯示。瀏覽器顯示該類型的錯誤。

Error: [ng:areq] http://errors.angularjs.org/1.3.9/ng/areq?p0=safeCtrl&p1=not%20aNaNunction%2C%20got%20undefined at Error (native) at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:6:416 at Qb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:19:417) at sb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:20:1) at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:76:95 at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:57:257 at s (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:7:408) at v (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:57:124) at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:52:9) at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:51:118

+0

如果Customer.cshtml應該是一個視圖,它不應該是一個完整的HTML文檔。剝離頂級html和body標籤,所以頂級標籤是div。 – 2015-01-21 04:17:55

回答

0

我並不完全相信你的問題是什麼,但

的第一件事情,我會注意的是,你應該通過從狀態控制器。你可以有.state('statename', { templateUrl: 'templateName', controller: controllerName });

此外,你可以通過使用狀態配置對象內的url標籤來指定你想要與路由關聯的url。現在

.state('sideMenuCustomerDivition', { url: '/route1', templateUrl: 'Main/Customer', controller: 'safeCtrl' });

如果你導航到該路徑與URL/ROUTE1,模板應插在用戶界面視圖

+0

模板已經路由到查看。我的問題是我不能將值分配到第二視圖。 – Saranga 2015-01-21 04:16:56

1

它可以幫助你!

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

app.config(function($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise('/'); 
    $stateProvider 
     .state('index', { 
      templateUrl: 'index.html', 
      controller: 'IndexCtrl' 
      }) 
     .state('index.test', { 
      url: '/', 
      templateUrl: 'test.html', 
      controller: 'TestCtrl' 
      }) 
     .state('app', { 
      templateUrl: 'app.html', 
      controller: 'AppController' 
      }) 
     .state('app.test2', { 
      url: '/test2', 
      templateUrl: 'test2.html', 
      controller: 'Test2Controller' 
      }) 
}); 

而另一個例子

$stateProvider 
    .state('index', { 
     url: '/', 
     views: { 
      '@' : { 
      templateUrl: 'layout.html', 
      controller: 'IndexCtrl' 
      }, 
      '[email protected]' : { templateUrl: 'tpl.top.html',}, 
      '[email protected]' : { templateUrl: 'tpl.left.html',}, 
      '[email protected]' : { templateUrl: 'tpl.main.html',}, 
     }, 
     }) 
    .state('index.list', { 
     url: '/list', 
     templateUrl: 'list.html', 
     controller: 'ListCtrl' 
     }) 
    .state('index.list.detail', { 
     url: '/:id', 
     views: { 
      '[email protected]' : { 
      templateUrl: 'detail.html', 
      controller: 'DetailCtrl' 
      }, 
     }, 
     });