Angularjs 模式内的日期选择器不工作

Angularjs 模式内的日期选择器不工作,angularjs,angular-ui-bootstrap,Angularjs,Angular Ui Bootstrap,我正在尝试使用ui引导组件在模式中创建一个日期选择器。日期选择器必须发回一个格式为unix时间戳的日期 如果datepicker不在模式内(即选择日期时时间戳会更新),则此操作正常: 然后,我将指令放在一个模态中: 以下是控制器: .controller('MyCtrl', [ '$scope', '$modal', function ($scope, $modal) { $scope.open = function () { var modalInstance = $m

我正在尝试使用ui引导组件在模式中创建一个日期选择器。日期选择器必须发回一个格式为unix时间戳的日期

  • 如果datepicker不在模式内(即选择日期时时间戳会更新),则此操作正常:

  • 然后,我将指令放在一个模态中:

  • 以下是控制器:

    .controller('MyCtrl', [ '$scope', '$modal', function ($scope, $modal) {
        $scope.open = function () {
            var modalInstance = $modal.open({
                templateUrl: 'tplModal.html',
                controller: 'MyModalCtrl'
            });
        };
    }])
    .controller('MyModalCtrl', [ '$scope', '$modalInstance', function ($scope, $modalInstance) {
        $scope.required= {};
        $scope.disabled = function(date, mode) {
            return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
        };
        $scope.minDate = new Date();
        $scope.$watch('dt', function() { 
            if ($scope.dt) $scope.required.timestamp = Math.floor($scope.dt.getTime() / 1000); 
            console.log('timestamp: ', $scope.required.timestamp, '/ dt: ', $scope.dt);
        });
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);
    
    以及模态的html模板:

    <div class="modal-body">
        <div ng-model="dt">
            <datepicker min="minDate" show-weeks="false"></datepciker>
        </div>
        <div>
            dt <span class="uneditable-input span2">{{dt | date:'dd.MM.yyyy' }}</span>
            dt <span class="uneditable-input span2">{{ dt }}</span>
            timestamp <span class="uneditable-input span2">{{ required.timestamp }}</span>
        </div>
    </div>
    
    
    日期:'dd.MM.yyyy'}
    dt{{dt}
    时间戳{required.timestamp}
    
    在第二个版本中,当日期更改时,时间戳不会更新(就像$watch不工作一样)


    你知道怎么做吗

    您需要在
    ng模型
    表达式中使用著名的“点”,因为$modal正在为窗口内容创建一个trasclusion范围。换句话说,在控制器和modal的内容之间创建了一个作用域

    无论如何,在
    ng模型
    表达式中使用点(这是最佳做法)可以为您解决以下问题:

    <div ng-model="dt.value">
      <datepicker min="minDate" show-weeks="false"></datepciker>
    </div>
    

    在这里使用plunk:

    您必须应用与
    timestamp
    相同的技巧,并像我一样将其放在作用域上的对象中


    如果我不得不猜测你遇到了范围问题;其中,模态创建控制器的子范围。我在使用ng include时遇到了这个问题。我不知道你的dt是怎么定义的;但我的解决方案(在我的例子中)是将我的“简单值”放入控制器的对象中,这样它就可以在子范围中访问。[简单属性未被继承;但对象被继承]。
    $scope.dt = {};
    $scope.$watch('dt.value', function(newValue) { 
      if (newValue) $scope.required.timestamp = Math.floor(newValue.getTime() / 1000); 
      console.log('timestamp: ', $scope.required.timestamp, '/ dt: ', newValue);
    });
    
    $scope.picker = {
      dt: new Date(),
      timestamp: ''
    };