Can an entire object be bound to a model in an Angular controller function?

In TypeScript, I have defined the following Interface:

interface Person {
    Id: number;
    FirstName: string;
    LastName: string;
    Age: number;
}

Within a .html partial file, there is an Angular directive ng-submit="submit()" on a form element. An example element inside the form looks like this:

<input id="FirstName" name="FirstName" type="text" class="form-control" ng-model="FirstName" placeholder="Enter First Name" />

I want to be able to map the form values to the object argument in the submit call like so:

$scope.submit = (person: MyApp.Models.Person) => {
  //Access person values
}

The issue arises when inspecting the person value within the submit(); it comes up as undefined and not populated.

I am curious if it's feasible to automatically use and bind the multiple <input> form values from within the form element directly to the object argument known by TypeScript in the submit() method?

It might be that this is simply not possible, but I would like to inquire before resorting to manually retrieving each ng-model value and constructing a Person instance within the submit() function.

Answer №1

Yes, it is definitely possible. In fact, some experts even recommend this strategy.

Instead of directly using the members of the person object in your form elements (ng-model=FirstName), it's better to use dot notation with a person object (ng-model=person.FirstName). For instance, in your scope object:

$scope.person: MyApp.Models.Person;

and in your HTML markup (notice the difference in your ng-model):

<input id="FirstName" name="FirstName" type="text" class="form-control" ng-model="person.FirstName" placeholder="Enter First Name" />

By doing this, when you invoke your submit function, you can pass it the person model

ng-submit="submit(person)"

and within your submit function, the person parameter will have access to all the properties of the person object defined on your form.

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

What's the reason for this Ajax request taking such a long time to display on the

My webpage features dynamic content created by Ajax, refreshing every 30 seconds with new information from the database. The PHP side of things seems to be functioning properly, but I suspect there might be an issue either in my JavaScript code or within t ...

Is there a way to verify the selection of all mandatory fields prior to concealing a div?

var count = 2; $('.next').click(function() { var requiredField = true; $(this).siblings('table').find('tbody tr:nth-child(' + count + ') td').each(function() { var element = $(this).find('center').f ...

Vue 3 - Child Component Script Not Updating with Reactive Prop Changes

I am facing an issue where I am trying to pass a reactive data as a prop to a child component in Vue 3. The data updates correctly in the child component's template, but it does not reflect in the child component's script. In the parent component ...

Utilizing NodeJS, Express, and the Jade template engine to insert JSON data into

While trying to follow a tutorial on NodeJS, Express, Jade, and MongoDB, I ran into a frustrating issue. Despite reading through various forum threads and attempting the suggested solutions, I still can't seem to make the POST command work. Please di ...

Tips for integrating Passport with an Angular Node application

Currently, I am in the process of building a basic blog website using a combination of angular.js + node.js and mongodb, incorporating an express template. In my project, I am utilizing $http from an angular controller to send a POST request to an API nam ...

A guide on incorporating Vue.js into a Hexo static site generator

Exploring the use of vue.js within my hexo theme has sparked my interest. Can anyone guide me on how to compile my .vue files for both development and production environments? It's worth mentioning that I intend for vue.js to operate on the client sid ...

ajax call triggering knockout foreach update

Within my ViewModel, I have defined a variable called self = this; Another foreach binding is working in my code, but it is not within an ajax request. The initial UI load is functioning correctly. I have confirmed that self.wikiData is being updated by ...

Could implementing a click/keydown listener on each cell in a large React datagrid with thousands of cells impact performance?

Years ago, before the advent of React, I mastered linking events to tables by attaching the listener to the <tbody> and extracting the true source of the event from the event target. This method allowed for a single listener for the entire table, as ...

How to Change a Property in a Child DTO Class in NestJS with Node.js

I am working with enums for status: export enum Status { Active = "Active", Inactive = "Inactive", } Additionally, I have a UserStatus enum: export enum UserStatus { Active = Status.Active, }; There is also a common dto that inc ...

Ways to dynamically emphasize text within ngFor loop

Within my ngFor loop, I have a set of rows. <div *ngFor="let block of data;"> <div class="class-row"> <div class="left">A Label:</div> <div class="right">{{block.key1}}</div> </div> <div class="clas ...

Fetching content-type in React code locally executed

I am a beginner in front end development and I need help with setting the "application/json" content-type and gzip content-encoding in the fetch call for my locally run React code. Can anyone provide guidance on this? const data = await fetch(url, { ...

Issue with Mongoose's deep population feature not functioning as expected

I'm currently working on a web application that requires the use of mongoose-deep-populate. I've already installed it using npm, but unfortunately, I keep encountering the following error: Error: Plugin was not installed at Query.deepPopulate (/ ...

Is there a way to utilize the child component's method?

I am looking to access a child component's method from the parent in Vue.js. To achieve this, I plan on using $refs. Code Example: <template> <div>Parent!</div> </template> Script: <script> Vue.component('c ...

JavaScript payload object's name

Here is the data I have received. {name: "Sinto 6", val: {…}, line: "Sinto 6"} line: "Sinto 6" name: "Sinto 6" val: AvgMachTime: 253 AvgManTime: 1343 CollectMachTimer: 359 CollectManTimer: 108 CycleTimeMach: 359 Cy ...

"Implementing a feature in Django that clears the input field upon clicking the clear button

I am working on a Django HTML file that includes an input field and a clear button. I need to clear the input field when the clear button is pressed. Is it necessary to use JavaScript for this task, or is there another way to achieve it? <form> ...

Does jQuery UI Layout have compatibility issues with jQuery version 3.3.1?

jQuery UI Layout is compatible with older versions of jQuery, but not with newer versions... Here is a working example: <html> <head> <script src="http://layout.jquery-dev.net/lib/js/jquery-1.4.2.js"></script> <script ...

establish connections within dynamic data table entries

As a newcomer to Javascript, I've been struggling with a particular question for some time now. The challenge at hand involves a dynamic jQuery table where I aim to hyperlink one row of the table data to a .php page in order to perform certain querie ...

Trouble with Jsonp when using the getJSON function in jQuery

$(document).ready(function() { $.getJSON("http://quanta.net16.net/wordpressnew/test.php?jsoncallback=?", function(data) { alert('swag'); }); }); This is the JSON request I'm making and my data is properly contained within ?({object} ...

What is the best way to extract a single item from an array in Javascript and assign it to a different variable

I am currently dealing with an array of objects structured like this: contacts: [ { id: 1, name: "John Doe", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="066c696e6846616b676f6a2865696b">[emai ...

save function ajax failure

I have created a function that adds a row after confirmation. The issue is that after submitting, the tables do not reload and show an error alert. In reality, the data is successfully saved but I need to refresh the page for the table to reload. Below is ...