"The controller seems to always return an 'undefined' value for the Angular

Within my project, I have implemented ui-view with the following structure:

<main class="content">
    <div class="inner" ng-controller="OrderPageController">
        <div ui-view="main-info"></div>
        <div ui-view="comments"></div>
        <div ui-view="bids-list"></div>
        <div ui-view="add-bid"></div>
    </div>
</main>

In the add-bid view:

<form name="newbidform" novalidate ng-submit="postNewBid(newbidform);">
   <input type="text" ng-model="newbid.bid" required>
   <input type="submit">
</form>

When submitting the form, I encounter an issue where all of the required inputs are considered undefined. Here is how I attempt to check for validity:

$scope.postNewBid = function(form) {

console.log(form) // $valid and $invalid always undefined
console.log($scope.newbidform) // always undefined

        // check validity before send
        if (!form.$valid) {
            angular.forEach(form.$error, function (field) {
                angular.forEach(field, function(errorField){
                    errorField.$setTouched();
                })
            });
            $scope.failedSubmit = true;
            return false;
        } else {
            $scope.failedSubmit = false;
        } 
// other things if form is valid

The issue arises from the form always being undefined, whether looking at $valid/$invalid attributes or otherwise. I have tried various approaches such as using formName as a parameter in the function, setting $scope.formName variable (always undefined), and defining the controller twice - once within the controller itself on the form element:

<form name="newbidform" novalidate ng-submit="postNewBid();" ng-controller="OrderPageController">

While this workaround functions, it restricts access to other variables in the controller. Is there a proper way to retrieve form state within a controller in AngularJS? Thank you.

Answer №1

Is newbid the variable you're looking to retrieve?

Give this a shot:

<form name="newbidform" novalidate ng-submit="postNewBid(newbid);">

Answer №2

It appears that your form is nested within a child scope, possibly inside an element like ng-include or ng-if. This means that the form object is not accessible in the parent scope because it was created in the child scope by Angular.

To resolve this issue, you can simply initialize an empty object in the parent controller. This object will then be inherited by all child scopes. By ensuring the object exists beforehand, Angular won't need to create a new one and all properties will be added to the existing reference in the child scope.

$scope.newform ={};
$scope.submitForm = function(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

Looping through arrays within objects using NgFor in Angular 2/JavaScript

I have a custom object with data that I am iterating through using ngFor. Within this object, there is an array component that I also want to iterate through in a <li></li>. Currently, the output appears as one complete string within each < ...

Maintaining a consistent style input until it is modified

I'm currently dealing with this code (continuing from a previous question): input[type=submit]:focus { background-color: yellow; outline: none; } The issue I'm facing is that when I click anywhere else on the screen, the background color go ...

Vue.js combined with Video.js (MPEG-DASH) is throwing an error: (CODE:4 MEDIA_ERR_SRC_NOT_SUPPORTED)

I am facing an issue with Video.js when using it as a component in vue.js. I receive a .mpd link from a server and I want to display the video from that link. Following the example in the documentation of Video.js and Vue integration. Every time I call th ...

The Angular template-driven form featuring Material inputs will automatically reset itself when initialized

I am currently working on a simple template-based form in my application, utilizing material form fields. I have opted for this approach instead of a reactive one. The desired functionality is to display the form only when the user clicks on a button. Up ...

What is the best way to use jQuery to set the height of one div equal to another div

Edited: I am facing a situation with a 3-column layout - Column A, Column B, Column C. The height of Column A is dynamic and I need to set the same height for both Column B and C. For instance, Column A contains a tabbed panel with varying heights based ...

Is there a way to automatically adjust the position of a tooltip div based on its location relative to the screen in AngularJS?

I've implemented AngularJs to create a grid with repeated li elements, each displaying a description box when hovered over. However, I am facing an issue where the description box goes off-screen when hovering over items on the right side of the grid. ...

Executing unit tests involves invoking a controller function using Karma and Jasmine

Here is the code for my angular controller: angular.module('authoring-controllers', []). controller('NavCtrl', function($scope, $location, BasketNavigationService) { $scope.test= function() { $scope.testVar = ...

Implementing Javascript to insert IFRAME into the DOM

I'm looking to incorporate an iframe into my webpage. The iframe needs to link to a specific URL. I attempted to add the following code to my HTML, but it's not functioning as expected: document.createElement('<iframe src='http://ex ...

Azure App Service for Mobile and Web applications

Currently, I am in the initial stages of assessing and designing the potential architecture for a Mobile App (using Xamarin) and a Web App (most likely using Angular). The Azure App Service appears to be a suitable option for hosting these services. Howeve ...

Let's leverage the power of Node.js and Express with the Jade templating

My goal is to iterate over an array within a jade layout file named lessons.jade: each lesson in myLessons ul.nav.pull-center: li.dropdown.nav.text-center .btn.btn-default.dropdown-toggle.btn-lg.btn-block(data-toggle="dropdown ...

The error message encountered is: "TypeError: Unable to access the 'apply' property of an undefined object within the

Currently in the process of developing a node js application with the integration of DHTMLX Scheduler feature on one of the pages. However, my progress is hindered by a recurring issue upon loading the page, resulting in the following error message: TypeE ...

Guide to finding and saving email addresses from a string output: extracting and storing each one individually in a text file

After collecting data from multiple sources, the output I obtained is as follows: "addressId":"132234","businessEntryCount":2026},{"district":"Nordend-West","districtSlug":"frankfurt-am-main- ...

Show the current time of a track using VueJS

Is there a way to have the time continuously update itself without needing to click on anything? When I press play, I want the seconds to automatically start updating from 0:00 until the end of the audio track. Currently, the time only updates when clicked ...

implement an angular directive to apply a CSS element

I am utilizing AngularJS and ng-repeat to populate a dynamic list of studies. This list has the capability to toggle into child elements of each item, creating an accordion-style toggle list that can go up to three levels deep for each list item. I am curr ...

Begin the initial function again once the second function has been completed

I have 2 functions in my code: function DisplayAltText(){ var CurrentPhoto = $("#DisplayPhoto img").attr("src"); if( $.browser.msie ) { IECurrentPhoto (CurrentPhoto); } if ($(".PhotoGallery img[src='" +CurrentPhoto+ "&a ...

The concept of global object/scope and circular references in undefined cases

Trying to make sense of the outcomes from these few experiments : Experiment number 1 (in a new command line) : > _ ReferenceError: _ is not defined at repl:1:2 at REPLServer.self.eval (repl.js:110:21) at Interface.<anonymous> (repl. ...

Encounter issue when using GAS withSuccessHandler function

I've developed a Google Sheets add-on that utilizes a modal dialog for the user interface. I encountered an issue with the success handler not running as expected, so I created a basic test interface to troubleshoot the problem. After the server-side ...

AngularJS: How to detect when the user has scrolled to the bottom of a div and trigger an event?

I am attempting to trigger an event when the scroll bar reaches the end. After searching, I came across this example. Below is my code, however, it seems that the loadmore() function is not being called at all. The console statements display the following ...

What is the proper syntax for Angular 2 form element attributes?

As I was browsing through this insightful article, I came across the following snippets: <input type="search" [formControl]="seachControl"> and <input type="text" formControlName="street"> This made me ponder on the correct syntax for ...

A customizable and adaptable Tetris-inspired CSS design perfect for any screen size

If we imagine having a block like this: <div class="block"></div> There are different sizes of the block: <div class="block b1x1"></div> <div class="block b2x1"></div> <div class="block b1x2"></div> For i ...