gpt4 book ai didi

javascript - 如何在 angularJS 中查看日期对象

转载 作者:行者123 更新时间:2023-11-30 00:30:30 25 4
gpt4 key购买 nike

是否可以在 angularJS 中观看日期?我有一个包含日期的 ng-model,我想观看它,所以当它的月份、年份或日期发生变化时,我会收到通知。

scope.$watch('ngModel',function(newVal){
console.log(newVal);
},true);

我有一个 Controller ,我在其中为指令提供了一个日期对象。现在我想看那个日期对象。

module.directive('Datepicker',function(){
return {
restrict:'E',
scope:{
ngModel:"="
},
link: function(scope, element, attrs) {
//..other code here
scope.$watch('ngModel', function(newVal) {
console.log(ngModel);
}, true);
}
}
});

最佳答案

我相信你在指令中,那么你应该使用 attrs.ngModel 而不是 ngModel ,它应该在指令的链接/编译函数中

代码

app.directive('myDirective', function() {
return {
'restrict': 'E',
scope: { ngModel: '='},
link: function(scope, element, attrs, ngModel) {
//..other code here
scope.$watch('$parent.'+ attrs.ngModel, function(newVal) {
console.log(newVal);
}, true);
}
};
});

关于javascript - 如何在 angularJS 中查看日期对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29751161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com