Javascript 如何在angularJS中查看日期对象

Javascript 如何在angularJS中查看日期对象,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,可以用angularJS观看约会吗? 我有一个ng模型,其中包含一个日期,我想看它,所以当它的月,年或日的变化,我得到一个通知 scope.$watch('ngModel',function(newVal){ console.log(newVal); },true); 我有一个控制器,我给一个指令一个日期对象。 现在我想看那个约会对象 module.directive('Datepicker',function(){ return { restrict:'E', scope:{

可以用angularJS观看约会吗? 我有一个ng模型,其中包含一个日期,我想看它,所以当它的月,年或日的变化,我得到一个通知

scope.$watch('ngModel',function(newVal){
    console.log(newVal);
  },true);
我有一个控制器,我给一个指令一个日期对象。 现在我想看那个约会对象

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);
  }
 }
});

您的ngModel应作为$scope提供:

function myController($scope){
   $scope.today = new Date()
      $scope.$watch('today',function(newVal,oldVal){
         //code goes here
      },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);
        }
    };
});

使用模型名代替NgModel。@user3093036您能分享您的更多指令代码吗?您使用的是隔离作用域吗?我确实使用了隔离作用域,但您的示例有效,但手表返回atrr对象吗?@user3093036请添加代码,我无法预测代码中发生了什么,请尝试执行
“$parent”。+attrs.ngModel
@user3093036
缺少
$parent.
,看一看Updates这个值实际到达控制器了吗?