momentjs、angularjs、类型脚本、指令和绑定
我想在矩变量上绑定一个输入type=“date”值。但它不起作用。这是我的密码: HTML:momentjs、angularjs、类型脚本、指令和绑定,angularjs,angularjs-directive,typescript,momentjs,Angularjs,Angularjs Directive,Typescript,Momentjs,我想在矩变量上绑定一个输入type=“date”值。但它不起作用。这是我的密码: HTML: <input class="form-control" type="date" name="birthday" placeholder="MM/DD/YYYY" ng-model="vm.birthday.value" required format-date> 错误: TypeError:this.value.isValid不是函数 在DateField.isValid(localho
<input class="form-control" type="date" name="birthday" placeholder="MM/DD/YYYY"
ng-model="vm.birthday.value" required format-date>
错误:
TypeError:this.value.isValid不是函数
在DateField.isValid(localhost:50106/Views/core/main.min.js:1409:78)
在Object.fn[as get](eval at(localhost:50106/Views/core/libs/angularjs/angular.min.js:212:283),
:4:322)
位于n.$digest(localhost:50106/Views/core/libs/angularjs/angular.min.js:130:71)
按n.$apply(localhost:50106/Views/core/libs/angularjs/angular.min.js:133:236)
在$$debounceViewValueCommit处(localhost:50106/Views/core/libs/angularjs/angular.min.js:264:243)
$setViewValue时(localhost:50106/Views/core/libs/angularjs/angular.min.js:263:482)
在HTMLInputElement.l(localhost:50106/Views/core/libs/angularjs/angular.min.js:162:479)
在HTMLInputElement.b.event.dispatch(localhost:50106/Views/core/libs/jquery/jquery-1.9.1.min.js:3:28337)
在HTMLInputElement.v.handle(localhost:50106/Views/core/libs/jquery/jquery-1.9.1.min.js:3:25042)(匿名
函数)@angular.js:12450(匿名函数)@
angular.js:9237n.$digest@angular.js:15777n.$apply@
angular.js:16030$$debounceViewValueCommit@
angular.js:25318$setViewValue@angular.js:25290l@
angular.js:21611b.event.dispatch@jquery-1.9.1.min.js:3v.handle@
jquery-1.9.1.min.js:3 angular.js:12450 TypeError:this.value.isValid
这不是一个函数
在DateField.isValid(localhost:50106/Views/core/main.min.js:1409:78)
在Object.fn[as get](eval at(localhost:50106/Views/core/libs/angularjs/angular.min.js:212:283),
:4:322)
位于n.$digest(localhost:50106/Views/core/libs/angularjs/angular.min.js:130:71)
按n.$apply(localhost:50106/Views/core/libs/angularjs/angular.min.js:133:236)
我不明白,就像我的指令没有被使用一样。如果我理解得很好,该指令(我从stackoverflow的另一个Q&A中获取并改编)必须在绑定上返回一个矩变量
谢谢。你试过了吗?没有。事实上,我更愿意理解为什么它不起作用,而不是使用大量的库。但是谢谢,如果我真的找不到解决办法,我会去看看的。
class DateField implements IField {
constructor(dateMin: moment.Moment, dateMax: moment.Moment) {
this.dateMin = dateMin;
this.dateMax = dateMax;
this.value = moment();
}
value: moment.Moment;
dateMin: moment.Moment;
dateMax: moment.Moment;
isValid() {
return this.value !== undefined && this.value !== null && this.value.isValid() && this.value > this.dateMin && this.value < this.dateMax ;
};
}
testApp.directive("formatDate", function () {
return {
require: 'ngModel',
link: function (scope: any, elem: any, attr: any, modelCtrl : any) {
modelCtrl.$formatters.push(function (modelValue) {
if (modelValue !== undefined && modelValue !== null && moment(modelValue).isValid()) {
return moment(modelValue);
}
else {
return moment();
}
});
}
};
});