由于以下原因无法实例化模块[模块名称]:错误:[$ injector:modulerr]

时间:2017-05-19 10:45:09

标签: javascript angularjs momentjs angular-moment

我在向模块moment.js中注入myApp时出现问题。我跟踪了 https://github.com/urish/angular-moment https://www.npmjs.com/package/angular-momentjs的文档,但无济于事。阅读其他类似的问题和答案,可能是由于错误的synatx,但我无法看到它。有什么想法吗?

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.min.js"></script>
<script src="scripts/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="scripts/angular-moment.js"></script>
<script src="scripts/app.js"></script>
<main ng-app="myApp" ng-controller="appCtrl">
    <div class="container">             
        <section class="ng-view">

        </section>
    </div>
</main>
var app = angular.module('myApp', ['ngRoute', 'angularMoment']);

app.controller('transCtrl', ['$scope', '$http', 'moment', function($scope, $http, moment){
    $http.get('../json/transactions.json')
    .then(function(response){
        $scope.transaction = response.data.transactions;
    }, function(errResponse){
        alert('error');   
    });

    $scope.sortType     = 'transId'; // set the default sort type
    $scope.sortReverse  = false;  // set the default sort order
    $scope.searchTrans   = '';     // set the default search/filter term

    $scope.date = new moment();

    console.log($scope.date)

}]);

2 个答案:

答案 0 :(得分:2)

由于您要从moment.js文件夹中加入angular-momentscripts,因此您应将这两个文件下载到scripts文件夹中,并确保您的服务器为scripts服务}文件夹为静态(可以通过检查浏览器的网络标记来确认。)

以下示例使用在线脚本并且工作正常。

&#13;
&#13;
var app = angular.module('myApp', ['ngRoute', 'angularMoment']);

app.controller('transCtrl', ['$scope', '$http', 'moment', function($scope, $http, moment) {
  $scope.date = new moment();

  console.log($scope.date)

}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.min.js"></script>
<!-- <script src="scripts/moment.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.0/moment.js"></script>
<!-- <script src="scripts/angular-moment.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.1/angular-moment.js"></script>

<div ng-app="myApp" ng-controller="transCtrl">
  <div class="container">

  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您需要先添加angular.min.js脚本。并在moment.js

之前添加angular-moment.js
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.min.js"></script>
<script src="scripts/moment.js"></script>
<script src="scripts/angular-moment.js"></script>
<script src="scripts/app.js"></script>