简体   繁体   中英

Injecting dependency angularMoment returning error

I'm trying to access moment.js API, injecting angularMoment as a dependency into my module but it's returning error:

Error: [$injector:unpr] http://errors.angularjs.org/1.6.4/$injector/unpr?p0=momentProvider%20%3C-%20moment%20%3C-%20transCtrl

I'm following the docs provided by https://github.com/urish/angular-moment but an error keeps being returned. Any idea why?


<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="https://cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.1/angular-moment.min.js"></script>
<script src="scripts/app.js"></script>

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

app.controller('transCtrl', ['moment', function(moment){

    $scope.date = new moment();  

}]);

Here is the detail on how to use angular-moment

https://github.com/urish/angular-moment

You have to include both moment.js and angular-moment.js in your application.

Well, after you included the moment.js script before angular-moment.min.js as I suggested in the comments to your question - You now have a simple error in your controller:

app.controller('transCtrl', ['$scope', 'moment', function($scope, moment){

You just need to inject $scope into your controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM