"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

Error: Trying to access the 'map' property of a null value, Using ReactJS with Axios

I'm currently developing a search bar feature where the user can input their query, hit the search button, and the request is stored in search. This request is then sent via Axios and the results are displayed at the end. Everything seems to be workin ...

Is it necessary to include @types/ before each dependency in react native?

I am interested in converting my current react native application to use typescript. The instructions mention uninstalling existing dependencies and adding new ones, like so: yarn add --dev @types/jest @types/react @types/react-native @types/react-test- ...

Unable to perform navigation during page load in a React.js application

I attempted to navigate to a route that should redirect the user back to the homepage when postOperations isn't set in the localStorage. To save time, please review the code snippet focusing on the useEffect and the first component inside return(). im ...

The slideUp function is not functioning as expected

I am trying to implement a slideUp effect on my webpage. Here is the code within my <body> tag: <script> $('#slide_up').click(function(){ $('p.text_study').slideUp('slow', function() { $ ...

How to align icon and text perfectly in a Bootstrap 5 Button

I am looking to align icons and text within a modal body: <div class="modal-body"> <div class="d-grid gap-2" id="someButtons"> </div> </div> This code snippet demonstrates how I insert buttons ...

Implementing hashing with resumable.js

Looking for a way to include hashing in resumable.JS by utilizing the "fileAdded" event hook. Any suggestions on how to add hash to each chunk? ...

Async reaction in MobX is a powerful tool for handling

Hey there, I am currently utilizing MobX in a store and faced with the need for an asynchronous reaction to occur when a computed value changes: class Store { @observable user; @observable something; @computed get firstParam () { ret ...

Error: The `Field` component encountered a failed prop type validation due to an invalid prop `component` after upgrading to MUI version

Encountered an error when migrating to Material ui v4 Failed prop type: Invalid prop component supplied to Field. in Field (created by TextField) This error points to the redux form field component export const TextField = props => ( <Field ...

When refreshing the page, redux-persist clears the state

I have integrated redux-persist into my Next.js project. The issue I am facing is that the state is getting saved in localStorage when the store is updated, but it gets reset every time the page changes. I suspect the problem lies within one of the reducer ...

What is the reason for the Circle to Polygon node module producing an oval or ellipse shape instead of a circle shape?

I've been experimenting with the npm package circle-to-polygon and I crafted the following code to generate a polygon that resembles a circle. const circleToPolygon = require('circle-to-polygon'); let coordinates = [28.612484207825005, 77. ...

"Hiding elements with display: none still occupies space on the

For some reason, my Pagination nav is set to display:none but causing an empty space where it shouldn't be. Even after trying overflow:hidden, visibility:none, height:0, the issue persists. I suspect it might have something to do with relative and ab ...

The custom pagination feature in Addsearch UI always default to displaying the first page of search

I am currently using addsearch-ui version 0.7.11 for my project. I have been attempting to integrate a customized pagination function based on this example template. However, I have encountered an issue where the arrow buttons always navigate to the first ...

Blocked the loading of scripts due to non-compliance with the Content Security Policy directive

Encountering an error with externally loaded scripts: Refusing to load the script 'https://code.jquery.com/jquery-3.4.1.slim.min.js' due to violating the Content Security Policy directive: "script-src 'self' https://ajax.googlea ...

Set the height of a div based on the height of another div

My challenge involves a textarea that dynamically expands as content is added to it. The structure of the textarea within a div element is illustrated below: <div id='sendMes' class='container-fluid'> <form action='#&apos ...

Challenges with parsing JSON using jQuery

I am attempting to retrieve data from a page that returns JSON in order to store it in an array. The current code is functional, but I am encountering difficulties when trying to pass the variable (which should contain the content) into the jQuery.parseJSO ...

Generating a fresh array by filtering out elements with specific properties using the .map() method

Within my React application, there exists an array containing key value pairs where each pair corresponds to a checkbox. Upon checking the checkbox, the value of the corresponding key switches between true and false. The structure of the data retrieved is ...

Reload React Page

I'm working on a function to update information in my database using React as the frontend. The issue I'm facing is that even though I can successfully update the item in the database, it doesn't reflect in real-time on the page unless I man ...

Instructions on including a directory in the package.json file for publication on npm

I am facing an issue when attempting to publish a module in the npm repository as it is not recognizing the 'lib' folder. Even though I have included it in the package.json file as shown below, the 'lib' folder contents are not being re ...

Retrieve the jQuery widget instance by selecting an element within the widget

I am currently developing a widget using the Jquery widget factory. The abbreviated version of the code looks something like this: _create: function(){ this.element.hide(); //hides original input element this.newInput=$("<input>"); //creates ...

Using Nestjs to inject providers into new instances of objects created using the "new" keyword

Is it possible to inject a provider into objects created by using the new keyword? For instance: @Injectable() export class SomeService { } export class SomeObject { @Inject() service: SomeService; } let obj = new SomeObject(); When I try this in my t ...