2016-07-26 135 views
0

使用AngularJS及其「間隔」選項,我能夠在時間中獲得秒數,但我是關於向2位數字添加毫秒的事情。以下是代碼。任何人都可以告訴我如何增加毫秒。如何在AngularJS中以毫秒爲單位獲取時間

AngularJs代碼

var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope, $interval) { 
    $scope.theTime = new Date().toLocaleTimeString(); 
    $interval(function() { 
     $scope.theTime = new Date().toLocaleTimeString(); 
    }, 1000); 
}); 

HTML

<div class="col-lg-2" ng-app="myApp" ng-controller="myCtrl"> 
<h3 style="color: blue;font-family: cursive;"><b>{{theTime}}</b></h3> 
</div> 

更新 - 我如何找到了解決辦法。

我只是做了以下更改。由於Pranjal

AngularJS代碼

var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope, $interval) { 
    $scope.theTime = new Date(); 
    $interval(function() { 
     $scope.theTime = new Date(); 
    }, 1); 
}); 

HTML

<div class="col-lg-2" ng-app="myApp" ng-controller="myCtrl"> 
<h3 style="color: blue;font-family: cursive;font-size: 20;margin-left: 20;"><b>{{theTime | date:'HH:mm:ss:sss a'}}</b></h3> 
</div> 

回答

1
<body> 
<script type="text/javascript"> 
    var app = angular.module('MyApp', []) 
    app.controller('MyController', function ($scope) { 
     $scope.CurrentDate = new Date(); 
    }); 
</script> 
<div ng-app="MyApp" ng-controller="MyController"> 

    <span>{{CurrentDate | date:'HH:mm:ss:sss'}}</span> 

</div> 
</body> 
+0

謝謝哥們它的工作。 – Jeeva

+0

但你有任何想法如何將其轉換爲12小時格式? – Jeeva

+1

而不是在首都「HH」,用小寫字母「hh」。 {{CurrentDate |日期:'hh:mm:ss:sss'}} Pranjal

相關問題