2016-01-21 34 views
0

我真的需要ngRoute模塊的幫助,因爲無論我做什麼 - 偏分量都沒有得到更新。 基本上這是我的文件夾結構:angular.js ngRoute模塊不刷新視圖

https://gyazo.com/a5c7ce7e52687f525d8d1ffd718c9a5a

的index.html菜單:


<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> 
<div class="container"> 
    <div class="navbar-header"> 
     <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"> 
      <span class="sr-only">Toggle navigation</span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
     </button> 
     <a class="navbar-brand" href="#/"><img src="images/logo.png" height=30 width=41></a> 
    </div> 
    <div id="navbar" class="navbar-collapse collapse"> 
     <ul class="nav navbar-nav"> 
      <li class="active"><a href="#/"> 
        <span class="glyphicon glyphicon-home" 
          aria-hidden="true"></span> Home</a></li> 
      <li><a href="#/aboutus"> 
        <span class="glyphicon glyphicon-info-sign" 
          aria-hidden="true"></span> About</a></li> 
      <li><a href="#/menu"> 
        <span class="glyphicon glyphicon-list-alt" 
          aria-hidden="true"></span> 
       Menu</a></li> 
      <li><a href="#/contactus"> 
       <i class="fa fa-envelope-o"></i> Contact</a></li> 
     </ul> 
    </div> 
</div> 


這些是index.html包括:

<script src="../bower_components/angular/angular.min.js"></script> 
<script src="../bower_components/angular-route/angular-route.min.js">       </script> 
<script src="scripts/app.js"></script> 
<script src="scripts/controllers.js"></script> 
<script src="scripts/services.js"></script> 

這些app.js的其中i配置routeProvider內容


'use strict'; 

angular.module('confusionApp', ['ngRoute']) 
    .config(['$routeProvider','$locationProvider', function($routeProvider, $locationProvider) { 
       $routeProvider 
      // route for the contactus page 
      .when('/contactus', { 
       templateUrl : 'contactus.html', 
       controller : 'ContactController' 
      }) 
     // route for the menu page 
     .when('/menu', { 
      templateUrl : 'menu.html', 
      controller : 'MenuController' 
     }) 
     // route for the dish details page 
     .when('/menu/:id', { 
      templateUrl : 'dishdetail.html', 
      controller : 'DishDetailController' 
     }) 
     .otherwise('/contactus'); 
}]);   

我使用

<ng-view></ng-view> 

到desplay諧音,但似乎沒有做工作。 我是Angular的新手,所以任何幫助或建議將非常感謝!

以防萬一有controllers.js 'use strict';

angular.module('confusionApp', []) 

.controller('MenuController', ['$scope', 'menuFactory', function($scope, menuFactory) { 

    $scope.tab = 1; 
    $scope.filtText = ''; 
    $scope.showDetails = false; 

    $scope.dishes= menuFactory.getDishes(); 

    $scope.select = function(setTab) { 
     $scope.tab = setTab; 

     if (setTab === 2) { 
      $scope.filtText = "appetizer"; 
     } 
     else if (setTab === 3) { 
      $scope.filtText = "mains"; 
     } 
     else if (setTab === 4) { 
      $scope.filtText = "dessert"; 
     } 
     else { 
      $scope.filtText = ""; 
     } 
    }; 

    $scope.isSelected = function (checkTab) { 
     return ($scope.tab === checkTab); 
    }; 

    $scope.toggleDetails = function() { 
     $scope.showDetails = !$scope.showDetails; 
    }; 
}]) 

.controller('ContactController', ['$scope', function($scope) { 

    $scope.feedback = {mychannel:"", firstName:"", lastName:"", agree:false, email:"" }; 

    var channels = [{value:"tel", label:"Tel."}, {value:"Email",label:"Email"}]; 

    $scope.channels = channels; 
    $scope.invalidChannelSelection = false; 

}]) 

.controller('FeedbackController', ['$scope', function($scope) { 

    $scope.sendFeedback = function() { 

     console.log($scope.feedback); 

     if ($scope.feedback.agree && ($scope.feedback.mychannel == "")) { 
      $scope.invalidChannelSelection = true; 
      console.log('incorrect'); 
     } 
     else { 
      $scope.invalidChannelSelection = false; 
      $scope.feedback = {mychannel:"", firstName:"", lastName:"", agree:false, email:"" }; 
      $scope.feedback.mychannel=""; 
      $scope.feedbackForm.$setPristine(); 
      console.log($scope.feedback); 
     } 
    }; 
}]) 

.controller('DishDetailController', ['$scope', '$routeParams', 'menuFactory', function($scope, $routeParams, menuFactory) { 

    var dish= menuFactory.getDish(parseInt($routeParams.id,10)); 
    $scope.dish = dish; 
}]) 

.controller('DishCommentController', ['$scope', function($scope) { 

    //Step 1: Create a JavaScript object to hold the comment from the form 
    $scope.customer_comments = {author:"", rating:"", comment:"", date:""}; 
    $scope.customer_comments.rating = 5; 

    $scope.submitComment = function() { 



     //Step 2: This is how you record the date 
     //"The date property of your JavaScript object holding the comment" = new Date().toISOString(); 
     var date = new Date().toISOString(); 
     $scope.customer_comments.date = date; 
     console.log($scope.customer_comments); 
     // Step 3: Push your comment into the dish's comment array 
     //$scope.dish.comments.push("Your JavaScript Object holding the comment"); 
     $scope.customer_comments.rating = Number($scope.customer_comments.rating); 
     $scope.dish.comments.push($scope.customer_comments); 


     //Step 4: reset your form to pristine 
     $scope.customer_comments = {name:"", number_of_stars:"", comment:"", date:""}; 
     $scope.commentForm.$setPristine(); 
     $scope.customer_comments.rating = 5; 
     console.log($scope.customer_comments); 


     //Step 5: reset your JavaScript object that holds your comment 
    } 
}]) 

;

+0

這是一個到app.js的鏈接 –

+0

你能不能顯示你的控制器的代碼 – aitnasser

+0

謝謝,剛把它加到了請求中! –

回答

0

似乎你的app.js文件夾是錯誤的。將它放在腳本文件夾之外,即與html頁面平行。

希望這個作品!

+0

試了一下,沒了( –

+0

る看到任何錯誤,在控制檯上?如果是,請分享這些 –

+0

部分A OK https://gyazo.com/071fdda8620218247fedbb3c85d0740a並沒有錯誤https://gyazo.com/9f37a4e9425cf32961fffc7d1a3a768b –

0

你缺少NG-應用=「confusionApp」在你的HTML代碼

+0

好友,它在那裏<!DOCTYPE html> –

+0

好友,它在那裏 –

+0

好的朋友,因爲它在您的代碼中提到 – aitnasser

0

,因爲它要替代以前的模塊,並創建一個新的,我們永遠不應該初始化angualr.module()在每個地方。

在這裏,當你在app.js,controller.js(也可能在service.js中)中創建模塊時,你無法實現所需的功能。

而不是在app.js中創建模塊一次。並使用該變量無處不在,即在controller.js和service.js

例如:var confusionApp = module('confusionApp',['ngRoute']);並在任何需要的地方使用「confusionApp」變量!

希望這應該工作!

+0

完成,沒有任何改變。 –

+0

你是否改變了結構,因爲我告訴你或還是一樣? app.js與htmls並行。雖然路由以這種方式給出了路徑,這就是爲什麼我強調它 –

+0

你好,哥們。去了它。所以:app.js - angular.module('confusionApp',['ngRoute'])/ services.js - angular.module('confusionApp')和controers.js angular.module('confusionApp',[])和controler.js應該是angular.module('confusionApp')! –

0

我非常感謝快速響應和幫助!我發現我犯的錯誤。在我的controllers.js中,我不應該包含'[]',所以它不會創建一個新的模糊,而是尋找現有的模糊。所以:app.js - angular.module('confusionApp',['ngRoute']),services.js - angular.module('confusionApp')和controers.js angular。模塊('confusionApp',[])和controler.js應該是angular.module('confusionApp')!這樣我們可以在app.js中創建模塊,然後在服務和控制器上使用它。謝謝大家