0

I am trying to update a date dynamically from a user clicking a forward or backward button, but can't seem to figure out how to make the data change from the view.

The variable date changes but not from the browser.

< July 31, 2017 >

Example pic

EDIT: I had originally put my methods inside the constructor (I don't have it that way in my code, but rather me mistyping it in the question here)

App Component

    export class AppComponent {
       date: Date;

       constructor () {
         this.date = new Date();
      }
      dateForward() {
        this.date.setDate(this.date.getDate() + 1);
      }

      dateBack() {
        this.date.setDate(this.date.getDate() - 1);
      }                
   }

HTML Template

<i (click)="dateBack()" class="fa fa-chevron-left" ></i>
<a>{{date | date:'MMM d, y'}}</a>
<i  (click)="dateForward()" class="fa fa-chevron-right"></i>

4 Answers 4

1

Beside not puting your methods inside your constructor you should pay attention to change detection and immutability this.date.setDate(this.date.getDate() + 1) will not trigger change detection, to enforce that you need this.date = new Date(this.date.setDate(this.date.getDate() + 1));, the change detector will notice the change only if you change to a different object entirely and not when you set an object properties, same thing with arrays

 constructor() {
    this.date = new Date();
 }

 dateForward() { 
    this.date = new Date(this.date.setDate(this.date.getDate() + 1));
 }

 dateBack() {
   this.date = new Date(this.date.setDate(this.date.getDate() - 1));
 }      
Sign up to request clarification or add additional context in comments.

Comments

0

You should not put your functions in your constructor. Instead, you should create methods in your class, this will allow you to call them in your HTML template.

public date: Date;

constructor() {
  this.date = new Date();
}

public dateForward = () => this.date.setDate(this.date.getDate() + 1);

public dateBack = () => this.date.setDate(this.date.getDate() - 1);

Comments

0

methods should not be inside constructor

date :Date;
  constructor() {
      this.date = new Date();
   }


    dateForward() {
       this.date = new Date(this.date.setDate(this.date.getDate() + 1));
    }
      dateBack() {
       this.date = new Date(this.date.setDate(this.date.getDate() -1 ));
      }      

Working Plunker link

Comments

0

in angular inside controller you can define a $scope variable lets say you call that variable date.

e.g. $scope.date = new Date().getDate();

Then inside your html you can access it

<div> {{date}} </div>

And whenever you click your call to action buttons you can change the value of this $scope variable and as soon as it will change the value of HTML will be updated automatically.

You can run the following code to see the example.

<!DOCTYPE html>
<html>
	<head>
		<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
	</head>
	<body>

		<div ng-app="myApp" ng-controller="myCtrl">

			<h1>{{date | date:'MMM d, y'}}</h1>

			<a href="#" ng-click="dateBack();">Back</a>
			<a href="#" ng-click="dateForward();">Forward</a>
		</div>


		<script>
			var app = angular.module('myApp', []);

			app.controller('myCtrl', function($scope) {
			    $scope.date = new Date();
			    
			    $scope.dateBack = function(){
			    	$scope.date.setDate($scope.date.getDate() - 1);
			    };
			    
			    $scope.dateForward = function(){
			    	$scope.date.setDate($scope.date.getDate() + 1);
			    };
			});
		</script>

	</body>
</html>

1 Comment

Thanks for the reply, but I'm not sure this is relevant for my issue? I'm using angular 4 whereas this seems to apply to angularjs. Forgive me if I'm mistaken, but the answer that Kld provided solved my issue.

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.