AngularJS has encountered an undefined form within its scope

In my main JavaScript file, I have a collection of functions that are used throughout my application. This JS file also handles the routing of the app.

main.js (MainController):

    $stateProvider
    .state('page1', {
        url : '/',
        templateUrl : 'page1.html',
        controller : "Page1Controller"
    })
    .state('page2', {
        url : '/',
        templateUrl : 'page2.html',
        controller : "Page2Controller"
    });`

Within Page 2, there is a form:

<div class="outer" ng-controller="MainController">
    <div class="page" ng-init="test();">
        <form id="page2_form" name="page2_form"> 
            <input type="text" id="field1" name="field1"/>
            <input type="text" id="field2" name="field2"/>
        </form>
    </div>
</div>

To validate the form using scope, I am currently getting an undefined result. I am using $scope.$watch to check this.

page2.js (Page2Controller):

    $scope.$watch('page2_form', function(page2_form) {
        console.log(page2_form);
    });

The output is "undefined". Even though the controller is loaded through routing, why is the form not being displayed?

UPDATE

Solution:

I added $scope.form = {}; to page2.js (Page2Controller) and modified the form like this:

<form id="form.page2_form" name="form.page2_form"></form>

Now, I can access fields in the form with

$scope.form.page2_form[fieldname]

Although, I'm puzzled as to why I have to do this now when in previous applications I did not need to specify form.form_name.

Answer №1

It appears that the issue you are facing is linked to JavaScript's ProtoTypal inheritance and the way Angular.js manages this in its 'ng-*' directives. For a more comprehensive understanding of this topic, I recommend checking out the following link: https://github.com/angular/angular.js/wiki/Understanding-Scopes

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

transferring information to d3.js

I have a json object structured in the following way { "tweet":[ {"text": "hello world"}, {"text": "hello world"} ] } When I check the console output of "data" in my code below, it shows me an Object tweet: Array[131]. However, when I l ...

Struggling to successfully pass data between pages using JSON

I'm currently working on HTML code for a view cart page where I display an item name. My goal is to be able to transfer this data, along with others later on, to a different HTML page that will automatically generate a list of multiple items with pric ...

Limitation on repetitive values in AngularJS push function

In need of a restriction to prevent duplicate values from being entered. The user can click on the Add button, which will then display a text field for inputting data. AngularJS will check if the inputted data is a duplicate or not. If it is not a duplicat ...

Tips for displaying a setCustomValidity message or tooltip without needing to submit the event

Currently, I am utilizing basic form validation to ensure that the email entered is in the correct format. Once validated, the data is sent via Ajax for further processing. During this process, I also check if the email address is already in use and whethe ...

What is preventing the listener from activating?

I came across some HTML code that looks like this: <form id="robokassa" action="//test.robokassa.ru/Index.aspx" method="post"> <input type="text" id="OutSum" name="OutSum" value="" placeholder="Сумма пополнения"> ...

Is there a way to trigger a function after Vue makes changes to the DOM?

There is a select box that displays either one of the two select boxes based on its value change: <form id="reportForm"> <select class="form-control chosen-enabled" v-model="selectedDataType"> <option value="First"&g ...

Attempting to start and restart an asynchronous function using setIntervalAsync results in a TypeError because undefined or null cannot be converted to an

Recently, I've been working on creating a web scraper that utilizes data extracted from MongoDB to generate an array of URLs for periodic scraping using puppeteer. My goal is to make the scraper function run periodically with the help of setIntervalAs ...

Maintain the chosen option after submitting the form

I have a survey. One of the pages is designed like this: <form method="POST"> <input type="hidden" value="true" id="x" name="x"> <table> <b>How important were the topics for your current and/or future business?</b> ...

obtain data from an array using Angular 5

i need assistance with retrieving values from an array. Below is my code snippet: this.RoleServiceService.getRoleById(this.id).subscribe(data => { this.roleData.push(data['data']); console.log(this.roleData); }) however, the resulting ar ...

Creating Custom Checkboxes with Individual States in React Native

Currently, I am facing an issue with the checkboxes associated with a list of products that are generated dynamically in a loop. All checkboxes are sharing the same state, so when one is checked, they all get checked. I am using React Native and the chec ...

It is impossible for Javascript to access an input element within a gridview

I have developed an asp.net page that allows a site administrator to select a user as the 'systems chair'. The page displays users in a gridview and includes a column of radio buttons to indicate who the current chair is or to change the assigned ...

Comparing document.getElementById and the jQuery $() function

Is this: var contents = document.getElementById('contents'); The equivalent to this: var contents = $('#contents'); Considering that jQuery is present? ...

What is the best way to add content to the ajax success code?

For my project, I have a lot of content in the success function of AJAX. I plan to move this content to a file called textcsc.js and use the include() method in AJAX. Initially, the code was working successfully: $(document).ready(function() { $(&apo ...

Understanding the fundamentals of parseInt() and radix conceptsORExploring

Could you clarify the concept of radix in relation to parseInt()? I'm struggling to grasp how the string argument varies with different bases/radix. ...

Is there a way to prevent my timer from resetting whenever I refresh the page?

Hey everyone, I'm new to coding and I could really use some help here. I have this code for a timer but I'm struggling to make it work properly even after refreshing the page. My goal is to keep the timer running smoothly, but I'm not sure w ...

What is the best approach to updating multiple rows instead of just the first row using the update button located on both sides using PHP and AJAX?

Starting fresh, so I might sound like a beginner with this question. How can I make use of the update button to update specific rows instead of just the top row? Every time I try to update, it only works for the top row. Here's the code snippet from ...

The error has not been handled properly and is being thrown at line 174 in the events.js file

I encountered an issue while trying to request data from an API, resulting in a crash on any Windows server. Can someone lend a hand with this problem? Here is a snippet of my code: app.get("/js/2806/api/products/getAllDrugs", (req, res) => { co ...

Managing JSON data sent using PHP

After exploring numerous syntaxes from various sources on the internet, I am hesitant to reveal my struggles as it may be hard to distinguish fact from fiction. So... This snippet shows a part of my HTML code: var jsonData = { address: 'address& ...

Vue.js - Exploring methods to append an array to the existing data

I am looking to structure my data in the following way: Category 1 Company 1 Company 2 Company 3 Category 2 Company 1 Company 2 Company 3 Below is my code snippet: getlist() { var list = this.lists; var category this.$htt ...

Combining objects in Angular Javascript by merging arrays

How can I merge matching objects from 2 array objects in Angular JS? Array 1: [ {"id":1,"name":"Adam"}, {"id":2,"name":"Smith"}, {"id":3,"name":"Eve"}, {"id":4,"name":"Gary"}, ] Array 2: [ {"id":1,"name":"Adam", "checked":true}, ...