javascript - 如何将这个 moment.js 代码从 node.js 运行到 angular.js Controller ?

标签 javascript angularjs node.js momentjs angular-moment

我在下面有这个 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/

相关文章:

javascript - AngularJS 搜索对象数组

angularjs - Angular Material md-card 边框底部带有向下箭头

node.js - 如何在 Node/Express.js 中禁用 HTTP 请求日志

javascript - 显示来自 mongodb 的所有数据并在 doT.js 模板引擎中渲染它

javascript - 引用错误: window is not defined Javascript

javascript - 在 url 中包含数据的方法

javascript - 如何合并 Angular js的两个对象数组?

javascript - 当 JavaScript 对象字面量嵌套时,this 是什么?

javascript - JWT 过期时注销用户

angularjs - Angular6 ngStyle 动态应用样式