简体   繁体   English

如何在angularjs中使用引导时间选择器

[英]how to use bootstrap timepicker with angularjs

I am using Eonasdan datetimepicker. 我正在使用Eonasdan datetimepicker。 This is my code 这是我的代码

<div class="form-group">
    <div class='input-group date' id='datetimepicker'>
        <input type='text' ng-model="dtime" name="time" class="form-control"/>
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-time"></span>
        </span>
    </div>
</div>

And this js for timepicker 和这个js的时间选择器

<script type="text/javascript">
    (function () {
        $('#datetimepicker').datetimepicker({
            format: 'LT',
            enabledHours: [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
        });
    })();
</script>

But in my controller I am not able to use $scope.dtime. 但是在我的控制器中,我无法使用$ scope.dtime。 It is giving undefined as its value. 它给undefined作为它的值。

if you want to use time picker from bootstrap you might want to wrap it a directive. 如果要使用引导程序中的时间选择器,则可能需要将其包装为指令。 By default you won't be getting the selected date in $scope since the value of input is updated externally by a javascript code. 默认情况下,您不会在$scope获得所选日期,因为input的值是通过javascript代码从外部更新的。 As a proof on concept you can refer to below link. 作为概念的证明,您可以参考下面的链接。

http://plnkr.co/edit/BEkkfwbhP7bYMtleQOzC?p=preview http://plnkr.co/edit/BEkkfwbhP7bYMtleQOzC?p=preview

In this example $scope.hello is set as Hello hence printing "Hello World" in UI. 在此示例中, $scope.hello设置为Hello因此在UI中打印“ Hello World”。 Value of hello is bind with input in UI and on click of button you can change the value but same will not be reflected in scope and you will still see "Hello World" printed. hello值与UI中的input绑定,单击按钮可以更改该值,但该值不会反映在范围中,并且仍会显示“ Hello World”。

Alternatively if you type something in input box then the same will be reflected in UI. 或者,如果您在input框中键入内容,则相同内容将反映在UI中。

I would suggest to refer to this earlier thread where pretty much same problem is discussed 我建议参考这个较早的线程,其中讨论了几乎相同的问题

How to wrap the datetimepicker js into AngularJS directive 如何将datetimepicker js包装到AngularJS指令中

There is a feature rich library that seamlessly integrates Bootstrap and AngularJS 有一个功能丰富的库,可无缝集成Bootstrap和AngularJS

https://angular-ui.github.io/bootstrap/#/datepicker https://angular-ui.github.io/bootstrap/#/datepicker

Your don't need to write directives from scratch. 您无需从头开始编写指令。 Always use Angular first approach. 始终使用Angular first方法。

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM