I have the below AngularJS code.
The current time is not displayed after executing the below code:
<html ng-app="a">
<head>
<title>Clock App</title>
<script src="angular.js"></script>
</head>
<body>
<h1>Clock Application</h1>
<div ng-controller="t"></div>
<p> The current time is </p>
<p ng-bind="timeString"></p>
<p>{{50+3}}</p>
</div>
<script>
var d = angular.module("a", []);
d.controller("t", function ($scope) {
var currentDate = new Date();
$scope.timeString = currentDate.toTimeString();
});
</script>
</body>
</html>
Keep the controller to parent tag. Then it will work.
Corrected code below:
<html ng-app="a"> <head> <title>Clock App</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script> </head> <body ng-controller="t"> <h1>Clock Application</h1> <div></div> <p> The current time is </p> <p ng-bind="timeString"></p> <p>{{50+3}}</p> </div> <script> var d = angular.module("a", []); d.controller("t", function ($scope) { var currentDate = new Date(); $scope.timeString = currentDate.toTimeString(); }); </script> </body> </html>
The timeString
value is not binding because the ng-bind
is not aware of the $scope.timeString
, since it is not inside the controller.
Try this
<div ng-controller="t">
<p> The current time is </p>
<p ng-bind="timeString"></p>
<p>{{50+3}}</p>
</div>
<html ng-app="a"> <head> <title>Clock App</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script> </head> <body> <h1>Clock Application</h1> <div ng-controller="t"> <p> The current time is </p> <p ng-bind="timeString"></p> <p>{{50+3}}</p> </div> <script> var d = angular.module("a", []); d.controller("t", function ($scope) { var currentDate = new Date(); $scope.timeString = currentDate.toTimeString(); }); </script> </body> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.