我在向模块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)
}]);
答案 0 :(得分:2)
由于您要从moment.js
文件夹中加入angular-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)
}]);
&#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;
答案 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>