无法将自定义时间设置为时刻选择器中的初始值

时间:2018-02-16 07:21:20

标签: angularjs angular-moment-picker

我正在尝试将datepicker的初始值设置为用户单击的图表上的时间。该值存储在ng-model中使用的变量中,但它仍然显示占位符。

<input class="form-control" ng-init="vm.input" ng-model="vm.input" ng-model-options="{ updateOn: 'blur' }" placeholder="Click here to select a date"
       moment-picker="vm.input" style="width:25em;height:25px;" format="LL LTS">
控制器中的

我传递的值最初显示:

scope.vm.input = moment(xScale.invert(newData[0])).format("ddd MMM D YYYY HH:mm:ss z");//Sun Feb 18 2018 05:16:29 

我错过了什么吗?我尝试了这个问题中给出的解决方案:How to set today date and time as default in angular-moment-picker。但它对我不起作用。

1 个答案:

答案 0 :(得分:0)

Your mistake is using scope and vm as this together in controller, description is:

<div ng-controller="ctrl as vm">
    {{vm.input}}
</div>

app.controller("ctrl", function() {
   var vm = this;
   //do not use scope.vm, vm is scope for this controller
   vm.input = "Hello World";
})

var app = angular.module("app", []);

app.controller("ctrl", function() {
  var vm = this;
  vm.input = moment().format("ddd MMM D YYYY HH:mm:ss z");
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl as vm">

<input class="form-control" ng-init="vm.input" ng-model="vm.input" ng-model-options="{ updateOn: 'blur' }" placeholder="Click here to select a date"
       moment-picker="vm.input" style="width:25em;height:25px;" format="LL LTS">
</div>