2014-01-10 142 views
0

以下是我的角碼。我搜索了很多錯誤的解決方案,提到的解決方案是包括['ngroute'],我已包括但仍然沒有解決錯誤。Angularjs注入模塊

下面是代碼:

的index.html

<!DOCTYPE html> 
<html ng-app="myapp"> 
<head> 
    <script type="text/javascript" src="angular.min.js"></script> 
    <script type="text/javascript" src="angular-route.min.js"></script> 
    <script type="text/javascript" src="controller.js"></script> 
</head> 
<body> 
    <h1>Amail</h1> 
    <div ng-view></div> 
</body> 
    </html> 

controller.js

var aMailservices=angular.module("myapp",['ngRoute']); 

    function emailRouteConfig ($routeProvider,$locationProvider) { 
    $locationProvider.html5mode(true); 
    $routeProvider. 
    when('/',{ 
    controller:'ListController', 
    templateUrl:'list.html' 
    }). 
    when('/view/:id',{ 
    controller:'DetailController', 
    templateUrl:'detail.html' 
    }). 
    otherwise({ 
    redirectTo:'/' 
    }); 
    } 

    aMailservices.config(emailRouteConfig); 


    messages = [{ 
     id: 0, sender: '[email protected]', subject: 'Hi there, old friend', 
     date: 'Dec 7, 2013 12:32:00', recipients: ['[email protected]'], 
     message: 'Hey, we should get together for lunch sometime and catch up.' 
     +'There are many things we should collaborate on this year.' 
     }, { 
     id: 1, sender: '[email protected]', 
     subject: 'Where did you leave my laptop?', 
     date: 'Dec 7, 2013 8:15:12', recipients: ['[email protected]'], 
      message: 'I thought you were going to put it in my desk drawer.' 
     +'But it does not seem to be there.' 
     }, { 
     id: 2, sender: '[email protected]', subject: 'Lost python', 
     date: 'Dec 6, 2013 20:35:02', recipients: ['[email protected]'], 
     message: "Nobody panic, but my pet python is missing from her cage." 
     +"She doesn't move too fast, so just call me if you see her." 
     } ]; 

    function ListController($scope){ 
    $scope.messages=messages; 
      } 

    function DetailController($scope,$routeParams){ 
     $scope.message=messages[$routeParams.id]; 
     } 

list.html

<table> 
<tr> 
    <td><strong>Sender</strong></td> 
    <td><strong>Subject</strong></td> 
    <td><strong>Date</strong></td> 
</tr> 
<tr ng-repeat='message in messages'> 
     <td>{{message.sender}}</td> 
     <td><a href="#/view/{{message.id}}"></a></td> 
     <td>{{message.date}}</td> 
</tr> 

detail.html

<div><strong>Subject:</strong>{{message.subject}}</div> 
<div><strong>Sender:</strong>{{message.sender}}</div> 
<div><strong>Date:</strong>{{message.date}}</div> 
<div> 
<strong>To:</strong> 
<span ng-repeat="recipient in message.recipients">{{recipient}}</span> 
</div> 
<div>{{message.message}}</div> 
<a href="#/">Back to message list</a> 
+2

有什麼錯誤,到底是什麼? – Beterraba

+0

當他嘗試在他的控制器中使用'$ routeParams'時,他很可能會得到一個「Angularjs注入器模塊器」。 –

+0

@Beterraba確切的錯誤未捕獲錯誤:[$ injector:modulerr] http://errors.angularjs.org/1.2.6/$injector/modulerr?p0=myapp&p1=TypeError%...t%2520Brothers%2Fangular%2Fegs% 2Fchap2%2Femail%2Fangular.min.js%3A17%3A178) – Tanmay

回答

1

我勸你把你的控制器模塊,否則他們將不會在$ routeParams回暖。依賴項被注入到你的模塊中;如果您的控制器沒有添加到模塊中,那麼他們將無法利用注入模塊的依賴。

執行以下操作:

angular.module("myapp",['ngRoute']).config(function($routeProvider,$locationProvider) {}) 
.controller('DetailController', function($scope,$routeParams){}); 
+0

您好@C Fairweather我在我的代碼中做了以下更改。我添加了以下行aMailservices.controller('ListController',function($ scope){ \t $ scope.messages = messages; }); aMailservices.controller('DetailController',function($ scope,$ routeParams){ \t $ scope.message = messages [$ routeParams.id]; });我仍然得到錯誤。不知道我做錯了什麼 – Tanmay