The error message "TypeError: object is not a function at new <anonymous>" indicates that the

I am having trouble adding 2 empty person objects to an array and then populating it. In Chrome, the error message

TypeError: object is not a function at new <anonymous>
appears. What could be causing this issue?

 $scope.person = {
      firstName: '',
      lastName: '',
      dateOfBirth: '',
      sex: '',
      nationality: ''
  };
  $scope.persons = [];
  $scope.persons.push(new $scope.person); // error
  $scope.persons.push(new $scope.person);

Answer №1

$scope.people.push(angular.copy($scope.person));
$scope.people.push(angular.copy($scope.person));

If you want a duplicate of a person object, you must use the angular.copy() method instead of the new keyword.

In programming languages like Java, you can create an object using a class, and once created, you can manipulate the object. However, you cannot create a new object directly from that object. Similarly, $scope.person is an object in AngularJS, and you should not use the new keyword with it.

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

Tips for concealing content within table cells

I am facing an issue with my form that contains a table. When the user clicks on the radio button labeled "No", I want the content of the subsequent cells in that row to become visible. However, when they click on "Yes", the content should be hidden again. ...

Tips on populating JSON data with AngularJS

Struggling to populate a searchable dropdown list with JSON data using AngularJS. After pushing the JSON data to the server, I'm able to receive a response when using the GET method. However, I'm unsure of how to query the JSON data and display i ...

It is not possible to assign a unique name to the custom attr() method

The code provided by someone else is working perfectly fine. However, when I try to rename the attr method, for example to attrx, I encounter an error. The error message I receive after renaming the method is: TypeError: arg.attrx is not a function Below ...

Enabling ngDisabled functionality within a directive

In my application, I am attempting to "overload" all inputs. Additionally, I want to utilize ngDisabled based on a flag within the directive's scope. This is what I have so far, but I am struggling with getting the ng-disabled attribute to work on th ...

Exploring the potential of jQuery and AJAX for dynamic content generation:

I have developed a jQuery plugin that pulls data from a JSON feed (specifically YouTube) and then presents the results in a DIV. Everything is working well until I need to display the results with different configurations, such as more videos or from anoth ...

Is there a way to resolve this issue? (An error occurred: TypeError - res.json is not a valid function)

When attempting to add an object to my MongoDB database const response = await fetch("/api/contact", { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json", }, }); I encounter the error message ...

Working with Associated Models in MongoDB using Sails.js

I am utilizing the Waterline ORM within Sails.js to construct a sample application with a model named 'Category'. Since a category can contain multiple subcategories, I have implemented a one-to-many association for this model: module.exports = ...

Reasons why `return()` and `onsubmit()` may fail when implementing a login form that redirects to a welcome page in HTML

Encountered an issue while developing a login form from HTML, CSS, and JS where the return statement was not functioning properly. Please provide code for implementing HTML embedded JavaScript. <!DOCTYPE html> <html> <head> <title& ...

Trigger Form Submission When Checkbox is Modified

Here is the PHP code I am working with: I need to update a value in the database immediately when a checkbox is checked or unchecked. I am looking to accomplish this using either PHP or Javascript. <?php session_start(); include("head.php"); inclu ...

Sending a function as a callback to children components in order to update a specific child component

I am currently working on developing a Navbar component that undergoes slight changes when a user logs in through a SignIn component. Here is an overview of how my application is structured: Initially, I have defined a state in the App component where aut ...

Numerous linkages facilitated by node-mongodb-native

Currently, I am working on a function aimed at inserting a document into a MongoDb database using the node-mongodb-native module. The function works perfectly fine, except when multiple documents are inserted back-to-back. To test how my function handles m ...

When trying to use express, the error message "d3.scale is

Having trouble integrating a c3-chart into my web application using node.js and express. The c3.js file is throwing the error message: TypeError: d3.scale is undefined Here is an excerpt from my layout.yade file: doctype html html head title= titl ...

What is the best way to modify specific data retrieved from an API using Angular?

After successfully listing some data from an API using ngFor, I am facing an issue with editing the data. Whenever I click the edit button, it edits the entire data instead of just the specific row. Below is the code snippet for reference: HTML <table ...

Unpacking jQuery's fancybox collection

Can anyone guide me to where I can locate the unpacked version of Jquery.fancybox-x.x.x.pack? I attempted to fix the code formatting manually in order to comprehend it better, but I'm still finding it challenging... ...

Tips on patiently awaiting the completion of resource loading

Seeking guidance from AngularJS experts as I develop an AngularJS application with a control suite comprising a loading screen and a showing screen defined in HTML. The data that needs to be manipulated is stored in JSON files, like the one below: { "St ...

Randomly undefined custom jQuery function

Appreciate your assistance in advance. I am facing a peculiar scope issue on a page where multiple charts are rendered intermittently. I have created a set of jQuery functions to display different types of charts based on the provided options. Each funct ...

Updating marginheight to "0" for iframe using JavaScript

While attempting to validate the code on my webpage, I encountered an error stating that the "marginheight" and "marginwidth" attributes of my iframe should be set in CSS instead. Through research, it appears that this cannot be achieved directly through C ...

Improved method for detecting click events

I am facing an issue with two calendars controlled by user input. Currently, if a user clicks to open one calendar and then clicks anywhere outside of it, both calendars close simultaneously because they are detecting each other as the target. I am looking ...

Steps for showing an item in ag grid in Angular

Cannot see the data being displayed. Here is the code snippet: Click here to view the code list.component.ts setAgGrid() { this.data.map((data: any) => { const date = new Date(data.date); const year = ('' + date.getFullYear() ...

methods for deleting an object from a AngularJS $scope variable

Looking for assistance in removing a single object from the $scope.Profile.address object. Below is the code and image for reference: <tr ng-repeat="x in Profile.addresses"> <td><input type="text" class="form-control" id="inputDefault" ...