The angularjs script encountered an issue when trying to access the property $scope.dropdownfield, as

I am working on a registration form page that includes a person_id field and a drop down list called condition_code_1. The person_id field is mandatory, while the condition_code_1 drop down list is optional for the user to select. The values for the drop down list are retrieved from a specialConds array which is sourced from an Oracle database field.

In my Condition_code_1 controller, I store the selected value of the drop down list in a data array. However, when the user submits the form without selecting anything from the list, AngularJS throws an error: Cannot read property 'condCode1' of null. How can I handle this situation and ignore the drop down list when it is not selected?

1) Condition_code_1 list

<select id = "condition_code_1"
        data-placeholder = "Please select"   
        ng-model         = "specialCond.condCode1"
        ng-options       = "t.condCode as t.condDesc for t in specialConds" chosen>
        <option value="">&nbsp;</option>
</select>

2) Condition_code_1 Controller

$scope.create = function () {
   var data = {};

   data.personId = $scope.person_id;
   data.conditionCode1 = $scope.specialCond.condCode1; 

   CondCodeSvc.create(data, function(res){
          jAlert('Create successful');
   }, errSvc.errorHandler);                   
};

Answer №1

The reason for this issue is that the variable data is not defined until you call the $scope.create() function, which results in it being undefined. Since it is not an object at that point, trying to access its properties will throw a JavaScript error.

To resolve this, you can modify your code as follows:

var data = {};
$scope.create = function () {

    data.personId = $scope.person_id;
    data.conditionCode1 = $scope.specialCond.condCode1; 

    CondCodeSvc.create(data, function(res){
       jAlert('Create successful');
    }, errSvc.errorHandler);                   
};

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

Identifying the HTML Hidden Attribute Using JavaScript Without Dependencies

As someone working in the analytics field, I often rely on CSS selectors to address various issues. Currently, I am faced with a task on a website where I need to determine whether a <p> element is hidden or visible. There are two possible scenarios: ...

Displaying a loading indicator as content loads within the iframe

How to Display a Loading Indicator During Content Loading Within an iFrame: 1) When a postback or submit event occurs within a form inside the iFrame ...

Filtering Arrays Using AngularJS

Imagine having an array object defined as follows: [{ name: 'a', number: 1 }, { name: 'b', number: 2 }, { name: 'c', number: 3 }] If you specifically want to retrieve the 'name' attribute wi ...

Suggestions for relocating this function call from my HTML code

I have been working on updating a javascript function that currently uses an inline event handler to a more modern approach. My goal is to eliminate the unattractive event handler from the actual HTML code and instead place it in a separate modular javascr ...

`Enhance Image with a Fresh Hue`

Let me explain my dilemma: I'm dealing with a two-tone png image - one tone is black and the other is transparent. Right now, I'm relying on the background color attribute to dynamically change the color of the transparent section. However, I ...

The challenge of transferring documents to the server

There is a form on the webpage where users can select a file and click a button to send it. <form enctype="multipart/form-data"> <input type="file" id="fileInput" /> <button type="button&quo ...

Able to display the value when printing it out, however when trying to set it in setState, it becomes

Within my form, there's a function that receives the value: _onChange(ev, option) { console.log(option.key) // Value of option key is 3 this.setState({ dropdownValue:option.key }) // Attempting to set state with 'undefined' as ...

Issues encountered with sending post requests to a yii2 application when using Angular4

After implementing the following code: this.http.post('http://l.example/angular/create/', {name: 'test'}).subscribe( (response) => console.log(response), (error) => console.log(error) ); I encountered an error on ...

When a Typescript object contains a key without a corresponding value, what is its significance?

In my experience, I've come across typescript objects that are defined in the following way: const MyObj = { myVar1, name1: myVar2 } Can someone explain the purpose of this object? I know that there is a key-value ...

Struggling with retrieving data from a file in Angular service while using node-webkit. Unable to make it functional

I've been struggling for a while and gone through numerous solutions, but I can't seem to figure out why this code isn't functioning: I have a requirement to pass data between controllers, so I created a service, right? (The data is coming ...

What is the process for displaying a list of all files within a folder?

I'm currently working on a project where I have a 'products' directory located in the same folder as my index.html file. My goal is to develop a script that can tally all the jpg files within the 'products' folder and then generate ...

Mark scheduled specific time periods

I need help removing booked time slots from the total time slots. How can I achieve this? Input: Actual time slots: [ '10:00-10:30', '10:30-11:00', '11:00-11:30', '11:30-12:00', '12:00-12:30', ...

Twice within a controller, alert is displayed in AngulasJS, OnsenUI, and Phonegap

I'm currently developing a mobile application using AngularJS, OnsenUI, and PhoneGap to package it for Android devices. However, I've encountered a strange issue where an alert box pops up twice when the application is run as a new process. I&ap ...

Error TS2403: All variable declarations following the initial declaration must be of the same type in a React project

While developing my application using Reactjs, I encountered an error upon running it. The error message states: Subsequent variable declarations must have the same type. Variable 'WebGL2RenderingContext' must be of type '{ new (): WebGL2 ...

React useEffect only retrieves the last element of an array object

I am facing an issue where React seems to only save the last element in my array. Even though I have two elements, when mapping over them, only the last element is being placed in the hook each time. React.useEffect(() => { if (bearbeiten) { handleCli ...

Tips for successfully passing an object via a Link

I am trying to pass an object through a Link component in react-router v6. Can someone please guide me on how to achieve this? Below is a snippet of my code where the user should be directed to another component. import React from 'react' import ...

JavaScript issues in FireFox and Internet Explorer when using JQuery

I have developed a PHP GD image for generating captcha images, saved as image.php. Each time it creates a unique captcha image. Inside the signup.php file: <img alt="" src="image.php"> <input type="button" id="btn" value="cannot read captcha"&g ...

Service dependency injection error encountered in the controller implementation

After creating a service and attempting to inject dependency into the controller, an error is being displayed: angular.js:12477 Error: [$injector:unpr] Unknown provider: ResultProvider <- Result <- ToolbarController http://errors.angularjs.org/1.4.7 ...

jQuery tab plugin does not open in a new browser tab when the 'ctrl' key is pressed

I have implemented the Jquery easy tab plugin on my webpage. When I perform a right-click on each tab and open it in a new browser tab, it displays correctly. However, if I press the ctrl key on the keyboard and click on a tab, it opens in the same browse ...

Best practices for incorporating and leveraging node packages with Laravel Mix

As I embark on my Laravel (v 8.x) Mix project, I am encountering challenges when it comes to incorporating JavaScript from node modules. To kick things off, here is a snippet from my webpack.mix.js: mix.js('node_modules/mxgraph/javascript/mxClient.mi ...