我在下面有这个 node.js 代码;
var moment = require('moment')
var myDate = new Date("2008-01-01");
myDate.setMonth(myDate.getMonth() + 13);
var answer = moment(myDate).format('YYYY-MM-DD');
我想在 angularjs 中运行它。我按照 https://github.com/urish/angular-moment 中的说明进行操作并添加了必要的链接和模块。
我想在我的 angularjs Controller 中运行代码。
angular.module('myApp.controllers', [])
.controller('OwnCtrl', ['$scope', '$http', '$timeout', '$window', 'configuration',
function($scope, $http, $timeout, $window, $configuration)
{
}
问题是我无法在 angularjs 中调用 var moment = require('moment')
,这与在 node.js 中不同。这如何在 angularjs Controller 中完成?
最佳答案
你不能在 Angular 代码上使用 Node.js 模块,但你可以安装 angular-moment,按照 README 中的说明注入(inject)文件和依赖项,并像这样使用它:
angular.module('myApp.controllers', []).controller('OwnCtrl', ['$scope', '$http', '$timeout', '$window', 'configuration', 'moment',
function ($scope, $http, $timeout, $window, $configuration, moment) {
var myDate = new Date("2008-01-01");
myDate.setMonth(myDate.getMonth() + 13);
var answer = moment(myDate).format('YYYY-MM-DD');
}]);
关于javascript - 如何将这个 moment.js 代码从 node.js 运行到 angular.js Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33449097/