0

我在注入moment.js我的模块时遇到问题myApp。我遵循了来自https://github.com/urish/angular-momenthttps://www.npmjs.com/package/angular-momentjs但无济于事。阅读其他类似的问题和答案,很可能是由于语法不正确,但我看不到。有任何想法吗?

<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)

}]);
4

2 回答 2

2

由于您包含moment.jsangular-moment来自scripts文件夹,您应该将这两个文件下载到scripts文件夹中,并确保您的服务器scripts作为静态文件夹服务(这可以通过检查浏览器的网络标签来确认)。

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

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)

}]);
<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>

于 2017-05-19T11:10:36.487 回答
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>
于 2017-05-19T10:46:14.217 回答