1

I am taking an edX course on AngularJS. I have run into a problem with one of the labs, and they don't post solutions, plus their forum seems rather weak, so here I am.

This is the first lab in the course. All the app is supposed to do is display the following:

Module 2 Homework

The Current Date is: “2016-05-26T16:53:22.313Z”

of course, replacing that time with the actual current time. All I get from my program is:

Module 2 Homework

The Current Date is: {{dateAndTime}}

My code is as follows:

<!DOCTYPE html>
<html ng-app="dateTimeApp">

<head>
  <title>Date Time App</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
    crossorigin="anonymous">
</head>

<body ng-controller="firstController">
  <div class="container">
    <h1>Module 2 Homework</h1>
    <h2>The Current Date is: {{dateAndTime}}</h2>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
  <script type="text/javascript">
    var dateTimeApp = angular.module('dateTimeApp', []);
    dateTimeApp.constant('myConfig', {dateTimeReadout: new Date()});
    dateTimeApp.controller('firstController', [
        '$scope', 'myConfig',
        function($scope, myConfig){
            $scope.dateAndTime = myConfig.dateTimeReadout;
        }
    ]);
</body>
</html>

I have also tried the following:

        dateTimeApp.controller('firstController', [
        '$scope',
        function($scope){
            $scope.dateAndTime = new Date();

I'm a bit lost here. Can someone help? Keep in mind that this is near the beginning of the course, so the answer should be very basic.

By the way, with the h2tag, not sure how to put Angular bit up there between quote marks.

3 Answers 3

1

Your scripts is not closed well.

it should like

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
  <script type="text/javascript">
    var dateTimeApp = angular.module('dateTimeApp', []);
    dateTimeApp.constant('myConfig', {dateTimeReadout: new Date()});
    dateTimeApp.controller('firstController', [
        '$scope', 'myConfig',
        function($scope, myConfig){
            $scope.dateAndTime = myConfig.dateTimeReadout;
        }
    ]);
    </script> <--- here you forget to close it.
Sign up to request clarification or add additional context in comments.

2 Comments

In the words of a famous yellow, beer-bellied, bald, donut-eating father: D'OH!!! I put that tag in and it worked! I can't believe it I didn't catch that. It's always the little things that get me.
@DavidTarvin :-) so maybe you can consider take this as the answer. haha~~~~
0

You need to close the script, its better to put to a separate file and load the reference,

 var dateTimeApp = angular.module('dateTimeApp', []);
 
dateTimeApp.controller('firstController', [
        '$scope',
        function($scope){          
            $scope.dateAndTime = new Date();
   }
]);
<html ng-app="dateTimeApp">
<head>
  <title>Date Time App</title>
</head>

<body ng-controller="firstController">
  <div class="container">
    <h1>Module 2 Homework</h1>
    <h2>The Current Date is: {{dateAndTime  | date:'medium'}}</h2>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
</body>
</html>

Comments

0

Go for that extra credit ;)

var dateTimeApp = angular.module('dateTimeApp', []);

dateTimeApp.controller('firstController', [
  '$scope',
  '$interval',
  function($scope, $interval) {
    $interval(function() {
      $scope.dateAndTime = new Date();
    }, 10);

  }
]);
<html ng-app="dateTimeApp">

<head>
  <title>Date Time App</title>
</head>

<body ng-controller="firstController">
  <div class="container">
    <h1>Module 2 Homework</h1>
    <h2>The Current Date is: {{dateAndTime | date:'MMM d, yyyy h:mm:ss:sss a'}}</h2>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.