Repeating events can be utilized to constantly update the Angular date

Users have the ability to set an end date for a scheduled event, but I would like them to also have the option to specify how many times they want the event to repeat before it ends. For instance, if they choose to repeat weekly for 4 weeks, I need to be able to calculate and update the end date accordingly. As a newcomer to angular, I'm not exactly sure how to approach this challenge.

http://jsfiddle.net/rXHzc/

This is what I was thinking:

function EventEditCtrl($scope) {
    init();

    function init() {
        $scope.event = {
            ActiveEndTimeDate: '',
            Occurrences: '',
            Repeated: ''
        }
    }

    $scope.updateEndDate = function () {
        //update event?
    };
}

Answer №1

To figure out the end date and set it to the model, you need to do some calculations. The tricky part is dealing with date operations in JavaScript. One helpful tool for this task is momentjs, a library that simplifies date manipulation...

$scope.updateEndDate = function () {
    var endDate = moment().add($scope.event.Repeated, parseInt($scope.event.Occurrences));
    $scope.event.ActiveEndTimeDate = endDate.format('YYYY/MM/DD');
};

Check out the code on this JSFiddle link.

This method involves using ngChange to detect changes in form values and trigger the update accordingly. However, it requires adding ngChange to every possible change trigger, leading to excessive logic within the HTML markup. A more streamlined approach would be to respond to model changes.

One way to achieve this is by utilizing a watch statement...

$scope.$watch('[event.Repeated, event.Occurrences]', function () {
    var endDate = moment().add($scope.event.Repeated, parseInt($scope.event.Occurrences));
    $scope.event.ActiveEndTimeDate = endDate.format('YYYY/MM/DD');
});

View the example at: http://jsfiddle.net/c92ud/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Interactive grid feature created with HTML Canvas

Imagine my surprise when I discovered that the latest version of Google Spreadsheets is now using a canvas tag to render the spreadsheet grid, unlike the traditional <table><tr><td> method used in the past. In the previous version, only ...

Using async/await with Middleware in Express

I'm struggling to grasp the concept of writing middleware in Express that uses async/await without leaving a floating Promise after execution. Despite reading numerous blogs and StackOverflow posts, it appears that there is a common pattern for utiliz ...

Implementing Observable in a function is a simple and effective way to

According to luwojtaszek answer in this topic: How to Export JSON to CSV or Excel - Angular 2 I tried out the following code: public exportAsExcelFile(json: any[], excelFileName: string): void { const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_s ...

What is the best way to save an object in a variable in MeteorJS/React for future retrieval?

This code snippet is located at the bottom of a component called 'Block'. export default theBlockContainer = createContainer(({ params }) => { return { voteStatus: Meteor.user()['listofvoted'], } }, Block); Although the ...

Using jQuery to gradually fade out my sidebar (div)

I have written the following code to hide my sidebar when the screen width is less than 1024 pixels, but it doesn't seem to be working. It worked fine on a previous website, so I'm not sure what the issue is here. Any help would be greatly apprec ...

Achieving the perfect sorting result in an array using Javascript

I am attempting to arrange the objects inside an array below in ascending order by their values and achieve the desired output as shown: var arr = [{"DOA Qty":"0.000665921017598927382910198160","LOS%":"0","FID Valid EC By Part":"0.004186044328301671376196 ...

Uploading Files with Vuetify 2 v-file-input and AxiosIn this tutorial, we

After researching extensively on the topic, I reviewed questions such as file-upload-in-vuetify and vuetify-file-uploads, but unfortunately, the solutions provided did not work for me. My current challenge involves utilizing Vuetify 2's <v-file-in ...

Invoke the view javascript function within a separate javascript script file

After an ajax call, my jquery template loads values rather than doing so on the initial view load. During page load, my javascript executes functions from a script.js file. I need this script.js file to then call another function located in the view - spe ...

Loading an HTML template dynamically into a pre-loaded div element

I need to dynamically load an HTML template into my index.html file. Now, I am looking to load another HTML template into the previously loaded template content. To clarify further: The index is loaded with a dashboard template, and the dashboard contains ...

Is there a way to iterate over the data from the returned Resource object using an angular.forEach loop in the controller

While browsing online, I encountered a similar issue. Despite my efforts, all I kept receiving was undefined errors in the console. The data is being fetched from the openweathermap API using the angular $resource factory. I am aware that I can extract th ...

The canvas in paper.js using angular.js will only update when the mouse hovers over it

I recently set up an application using angular.js. I am working on creating a menu using ng-repeat, where every link triggers a function within a directive that utilizes paper.js to draw text onto a canvas. However, I noticed that the canvas does not upda ...

Conceal the Vue router on a webpage

How can I hide the vue-router from my login page? I want to remove the menu on the Login page. Is it possible and how would I achieve that? Here is the code for the Login page: Login <template> <div> <h1>Login</h1> ...

What is the process for retrieving information from my Google Analytics account to incorporate into my website?

Imagine being the proud owner of Your website is equipped with a Google Analytics script that diligently gathers data about your valuable visitors. Now, you have a desire to set up a page views counter. How can you extract data from your own account? ...

Needing to utilize the provide() function individually for every service in RC4

In Beta, my bootstrapping code was running smoothly as shown below: bootstrap(App, [ provide(Http, { useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, helperService: HelperService, authProvider: AuthProvider) => new CustomHt ...

What is causing the delay in starting to play an audio track when it is clicked on?

I am facing an issue with my application and have created a minimum code example on StackBlitz to demonstrate the problem. The problematic code is also provided below. My goal is to have the Audio component play a track immediately when the user clicks on ...

The JavaScript-set value in a form field is not being transmitted to the PHP script within the $_POST array

Struggling to pass a JavaScript value to a .php script, and then on to a .txt script. It works fine with regular numbers, but when trying with the variable it fails, leaving the .txt file blank. Despite extensive research online, I can't seem to get i ...

I'm encountering difficulties in automatically populating the category field from an API

Hey there! I have set up a signup form and I am trying to automatically fetch categories from the server API to populate an input area. Everything seems to be in place, but for some reason, I am unable to retrieve the data. API: Here is the code I'm ...

Restricting the input on a React component to only accept alphabet characters from A to Z instead of allowing any keyboard

I am currently facing a challenge with understanding a specific component, particularly the allowForClassification value. This boolean value is passed down to a child component and decides whether a button is displayed or not. The issue arises when tryin ...

Utilize Moment to round a date either up or down

I am using Moment to compare two datetime values. Specifically, I am utilizing Moment.isSameOrBefore function. However, my two date values are slightly different due to milliseconds. I want these two values to be considered the same: var date1 = ' ...

ng-map vm.positions fail to refresh

I am currently utilizing the ng-map directive in conjunction with AngularJS. I have implemented the code below to generate markers. I store latitude and longitude values for each marker in an array, much like the examples shown here. Upon inspecting the lo ...