AngularJS: Utilizing a single 'partial form' for creating and updating articles

For the example, let's consider a simple form with just one text input. I would like to put it in a partial so that I can include other inputs and have a single form for both creating and updating the artist.

Partial Code

<input name="name" type="text" data-ng-model="artist.name" id="name">

Create Form

<section data-ng-controller="ArtistsController">
  <form name="artistForm" class="form-horizontal" data-ng-submit="create()" novalidate>
    <ng-include src="'/modules/artists/views/partials/form-artist.client.view.html'"></ng-include>
  </form>
</section>

Edit (Update) Form

<section data-ng-controller="ArtistsController" data-ng-init="findOne()">
  <form name="artistForm" class="form-horizontal" data-ng-submit="update(artistForm.$valid)" novalidate>
    <ng-include src="'/modules/artists/views/partials/form-artist.client.view.html'"></ng-include>
  </form>
</section>

In my client controller, I can create an artist :

$scope.create = function() {
  var artist = new Artists({
    name: this.name
  });
  artist.$save(function(response) {
    //success!
  }, function(errorResponse) {
    $scope.error = errorResponse.data.message;
  });
}

And I can update the name of an artist :

$scope.update = function() {
  var artist = $scope.artist;
  artist.$update(function() {
    //success!
  }, function(errorResponse) {
    $scope.error = errorResponse.data.message;
  });
};

The problem arises when creating an artist, as this.name is undefined. If I change the data-ng-model in the partial to data-ng-model="name", I can create an artist successfully, but the input does not populate with the name in the update form.

Any suggestions on how to effectively merge the two forms in one partial?

Answer №1

The information is stored within the artistForm object within the current scope.

All you have to do is modify:

name: this.name 

to

name: $scope.artistForm.name.$viewValue

By the way, using console.log($scope) can be very helpful for further investigation.

Answer №2

Upon reviewing the code, it appears that the artist object has not been included in the scope during the creation process. It is necessary to add the artist to a $scope variable. Additionally, in the partial, the correct syntax to use would be artist.name.

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

Adding an additional parameter in the ListItem onMouseOver function

Within my React code base, I have a material-ui ListItem that looks like this: <ListItem button key={currIndex} onMouseOver={handleOnMouseClickOnListItem}> The handler function in my code (using Flow typing) is as follows: const handleOnMouseClic ...

Adjusting the StrokeWidth in pixels/em units on fabricjs

When adding a rectangle or circle to the canvas and adjusting the strokeWidth using a range input, how can we determine the value of strokeWidth in pixels, em, or percent? For instance, if we set strokeWidth to 5 within a range of 1-10, how much does the a ...

Angular's $http service integrated with AWS API Gateway

My AWS API gateway is using Cognito temporary credentials. I am looking to configure the Angular native $http service to interact with the API while including the necessary aws4 authentication headers. I attempted to utilize https://github.com/mhart/aws4 ...

Creating a dynamic search feature that displays results from an SQL database with a dropdown box

Is there a way to create a search bar similar to those seen on popular websites like YouTube, where the search results overlay the rest of the page without displacing any content? I've searched extensively on Google and YouTube for tutorials on databa ...

Implementing a specialized CSS handler for Node.JS

Currently, I have set up a Node.JS server for a new project at my workplace. As part of the project, I have created an optimizer function that removes unnecessary elements such as tabs, newlines, and comments from HTML, JavaScript, and CSS files. Strangel ...

Troubleshooting angular.js error with ng-bind-html

I am experimenting with angular.js and trying to understand ng-bind and ng-bind-html. Here is the code snippet I am working on: <div ng-app="module" ng-controller="controller as ctrl"> <div ng-bind-html="ctrl.html"></div> </div> ...

What could be causing the invalid hooks error to appear in the console?

I'm completely stumped as to why this error is popping up when I try to use mutations with React Query. Any insights or advice would be greatly appreciated. Note: I'm implementing this within a function component in React, so it's puzzling ...

Can a background image flow into another div?

My goal is to use this specific image as the background image for this particular page. I want the background image to begin within the navbar navbar-default class. However, I'm facing an issue. I need the image to extend into the next div, which is d ...

Returning to the previous state in AngularJS when unchecking a checkbox

I'm a beginner when it comes to using angularjs HTML File: <div class='col-md-2 searchdisplay-col1' ng-controller="amenitiesController" style="margin-top:20px"> <h4>Amenities</h4> <label ng-repeat="facilityName ...

I obtained the binary tree output in the form of an object. How can I extract the values from this object and store them in an array to continue working on

Issue Statement In this scenario, you have been presented with a tree consisting of N nodes that are rooted at 1. Each node in the tree is associated with a special number, Se. Moreover, each node possesses a certain Power, which is determined by the count ...

Expanding box geometry from a single side in Three.js

Just starting out with three.js and my first task was to create a box geometry that could be increased from only one side. Issue : When increasing the width or height of an object, both sides automatically expand. Check out this jsFiddle Example I waste ...

jQuery floating sidebar that dynamically adjusts position based on content overflow

I've come across an interesting scenario with a fiddle that I'm working on: http://jsfiddle.net/yFjtt/1/ The main concept is to allow users to scroll past the header and have the sidebar 'stick' in place as they continue scrolling down ...

Selecting an option from the knockout dropdown menu

I implemented a language dropdown in layout.chtml using knockout js. <select id="Language" class="styled-select" data-bind="value: Language,options: locale, value: selectedLocale, optionsText: 'name'"></select> var viewModel = th ...

Creating a dropdown feature for menu items in Vue when the number or width of items exceeds the menu bar's limits

I am working on a navigation bar that displays menu items as tabs. One issue I am encountering is when the number of menu items exceeds the space available, I need to move the excess items into a dropdown menu (showmore) using Vue. Here is an example of t ...

What strategies can I use to ensure that I can successfully send 3,000 requests to the Google Drive API using node.js without surpassing

I'm currently assisting a friend with a unique project he has in mind. He is looking to create 3000 folders on Google Drive, each paired with a QR code linking to its URL. The plan is to populate each folder with photos taken by event attendees, who ...

When I click the login button, a .ttf file is being downloaded to my computer

I have encountered an issue with my web application where custom font files in .ttf, .eot, and .otf formats are being downloaded to users' local machines when they try to log in as newly registered users. Despite attempting various solutions, I have b ...

Mutating properties in VueJs

When I attempted to move a section for filtering from the parent component to a child component, I encountered this error message: "Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a ...

Translate CSS into JavaScript while maintaining selector understanding

I am interested in developing a tool that can read a .css file and generate a function that will accept an array of class names as input and output the corresponding styles for an element with those classes, represented as a JavaScript object. This tool wo ...

How can you create a jQuery fade in effect for a single <li> element

I'm in the process of developing a task management app that generates a new li element every time a user adds an item. However, I am facing an issue where fadeIn() is activating for every li on the page whenever a new item is added. Does anyone have s ...

Google Drive API generates new blank file named 'Untitled' when used with certain browser versions

Trying to utilize the 'browser' version of the Google Drive API, which seems to mostly adhere to Nodejs syntax. However, there are limited examples available beyond the basic hello world example for the browser. Currently, the goal is to create ...