同一个文件中有两个指令,这两个指令都依赖于moment.js库。当应用时,第一个指令(momentNow)看到矩函数,而另一个没有(值是未定义的)。
两个指令都在同一个模板中使用:
模板
<datepicker date-format="yyyy-MM-dd">
<!-- works correctly -->
<input required ng-model="vm.dateFrom" moment-now />
</datepicker>
<datepicker date-format="yyyy-MM-dd">
<!-- throws error -->
<input required ng-model="vm.dateTo" moment-then />
</datepicker>模块
;(function() {
"use strict";
angular.module('dateUtils', [])
.directive('momentNow', momentNow)
.directive('momentThen', momentThen);
function momentNow() {
return {
/* params are omitted */
link: function(scope, element, attrs, ngModel) {
console.log(moment);
var value = moment().format(scope.momentFormat || 'YYYY-MM-DD');
ngModel.$setViewValue(value);
ngModel.$render();
}
}
};
function momentThen() {
return {
/* params are omitted */
link: function(scope, element, attrs, ngModel) {
console.log(moment);
var moment = moment();
var format = scope.momentFormat || 'YYYY-MM-DD';
var value = (moment.hours() >= 20 ? moment.add('days', 1) : moment).format(format);
ngModel.$setViewValue(value);
ngModel.$render();
}
}
};
}());https://stackoverflow.com/questions/46035930
复制相似问题