AngularJS restricts inputs to be read-only

I'm facing an issue with readonly inputs in AngularJS. I have a select element that changes the values of readonly inputs through a script. However, when I attempt to display these values using ng-model in a table as {{ng-model}}, they don't appear.

<select name="well" onChange="Update(this.value)" ng-model="well" required>
<option value="Well-01">Well-01</option>
<option value="Well-02">Well-02</option>
<option value="Well-03">Well-03</option>
</select>
<label>Region:</label>
<input type="text" name="region" value="South" ng-model="region" disabled>
<label>State:</label>
<input type="text" name="state" value="Oklahoma" ng-model="state"  disabled>
<label>Field Office:</label>
<input type="text" name="office" value="Ringwood" ng-model="office" disabled>

Fiddle: http://jsfiddle.net/NKyps/7/

It displays {{well}} when I change it, but the inputs are not shown. Thanks for your assistance. PS: If anyone knows how to set a default option (e.g., Well-01) so that the inputs are filled when the document is loaded, please share.

Answer №1

It seems that the issue lies in the attributes not being properly bound to a model. When using {{variable}}, Angular expects them to be binding with a specific model.

I have included the scope model in your tags:

<form ng-controller="Sarapastrule" name="form">
</form>

Please review: http://jsfiddle.net/NKyps/9/

Answer №2

According to Bart, it seems you might be missing some fundamental AngularJS concepts and may benefit from reading the AngularJS tutorial available at http://docs.angularjs.org/tutorial.

In AngularJS, two-way data binding is essential. It handles updating the DOM while developers mainly focus on updating the model. Directly accessing value properties of input fields is not the recommended approach in AngularJS.

I have made modifications to your jsFiddle to work properly, but I do suggest going through the tutorial to grasp the underlying principles of AngularJS better.

You can view the updated version here: http://jsfiddle.net/BnqZS/1/

It's important to establish a controller first to manage the business logic. Within the controller, keep an eye on and update the model accordingly. Instead of:

if (choice == "Well-01") {
    region.value = 'South';
    state.value = 'Oklahoma';
    office.value = 'Ringwood';
}

You should set up a watch on the model connected to the option input field and use code like this:

$scope.$watch('well', function(choice) {
  if (choice == "Well-01") {
        $scope.region = 'South';
        $scope.state = 'Oklahoma';
        $scope.office = 'Ringwood';
    }

}

Answer №3

Your approach involves working outside of angular and performing custom DOM manipulation, which goes against the core principles of angular.js. I have provided an updated fiddle here for reference.

To resolve this issue, it is recommended to use a controller with a scope to define the values of your model.

function ExampleController($scope) {

    $scope.Update = function () {
        if ($scope.well == "Well-01") {
            $scope.region = 'South';
            $scope.state = 'Oklahoma';
            $scope.office = 'Ringwood';
        }

        if ($scope.well == "Well-02") {
            $scope.region = 'North';
            $scope.state = 'Montana';
            $scope.office = 'Sidney';
        }

        if ($scope.well == "Well-03") {
            $scope.region = 'North';
            $scope.state = 'North Dakota';
            $scope.office = 'Tioga';
        }
    }

    $scope.well = 'Well-01';
    $scope.Update();
}

The primary issue with your current design is the attempt to manage two separate models, one in JavaScript and another in the DOM. Angular.js is optimized for handling DOM manipulation on your behalf, simplifying the task of updating your model. Instead of manually updating the DOM, focus on creating a view that generates itself from a model, allowing modifications only to the JavaScript model ($scope variable).

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

Obtain precise JSON information using Node.js

Having limited experience with Angular JS and Node JS, I find myself in need of assistance. On the server side, I have multiple JSON files containing language translations. Based on a client's request for a specific language, such as French, I must re ...

Using AngularJS to serve $resource through https causes Angular to transition from https to http

After successfully setting up a ng-resource in my AngularJS project to fetch data from a REST API, everything was running smoothly in the development and testing environments, both of which operated over http. However, upon moving to production where the e ...

What is the best way to update the content of a particular div and its associated link ID whenever the link is clicked?

I need to update the content of a div by clicking on an href link that includes an id named data. The issue I'm facing is that I can't access the id value within my script because it's passed within a function. How can I pass the data variab ...

What significant alterations can be found in the architecture of Angular 2?

Hello, I am currently exploring Angular 2 and Stack Overflow. I am curious about the significant architectural differences between Angular 2 and its predecessor, Angular. In Angular, there were functions like $apply, $digest, $evalAsync, and others. Why we ...

Connected Date Selector

I am new to using Bootstrap and attempting to implement a linked datepicker with the following requirements: Display only the date (not the time). The default selected date on page load should be today's date for both the Datepicker and the text ...

Having trouble making an AJAX request work in AngularJS?

Just dipped my toes into the world of Angular (only a few hours in). Managed to tweak the demo to get close to what I need, but hitting a roadblock with my AJAX request. After trying a variety of fixes, one puts me in an endless loop (discovered that&apos ...

Issue with external JavaScript file being unresponsive on mobile browser

Hello everyone, hope you're having a great afternoon or evening I've encountered an issue with mobile browsers like Chrome Mobile and Kiwi where the external js file is not visible in the html file. The script.js file looks like this: alert(&ap ...

Establish a connection with the ejabberd server as well as the application server

I manage an ejabberd server and am currently working on implementing a chat module within my Angular/NodeJS application. Within my setup, the Angular app directly connects to the chat server. I have a roster consisting of 100 contacts, some marked as onli ...

What is the best way to change the format of a datetime?

While working with sailsjs(node.js), I have successfully retrieved all the data from a MySQL database and displayed it in a jtable. However, the date format is currently showing as YYYY-MM-DDTHH:mm:ss.000Z. I am looking to convert this format (YYYY-MM-DDT ...

SolidJS: "Value not recognized"

I have successfully retrieved the JSON value from the API path. Flask Code app = Flask(__name__) CORS(app) @app.route("/cats-matches", methods=['POST']) def cats_matches(): person = {"name": "John"} #This is not ...

Using AngularJS to pass a parameter in the ng-submit function

I'm currently working on a project using angular and passport for user authentication. I've been struggling to pass the logged-in user ID when trying to save a form. Here's what my code looks like: site.jade: div(ng-controller='sitesC ...

I am receiving a high volume of row data from the server. What is the best way to present this information on a redirected page?

There is a scenario where Page 1 receives a substantial amount of row data in JSON format from the server. The goal is to present this information on Page 2, which will be accessed by clicking on Page 1. To accomplish this task, JavaScript/jQuery and PHP ...

Issue with vuejs build process not incorporating relative paths into the final output

I'm encountering a problem trying to run my Vue.js app from the dist folder. After searching on this site, I came across a helpful thread titled Vuejs, Difficulties to build with relative path, which suggested the following solution: Create a "vu ...

What is the reason for JavaScript consistently returning the initial value as the result?

My current issue involves customizing Joomla article content using a module. I am attempting to hide a div until a user clicks on an input, such as a radio button labeled Test1. Once Test1 is selected, another hidden field within the div should display the ...

Implement jQuery to close multiple div elements

My dilemma involves a series of divs with different manufacturer IDs listed as follows: manufacturer[1], manufacturer[2], manufacturer[3], ... etc ... In attempting to create a JavaScript for loop to hide each div, I discovered that it was not achievab ...

Developing an uncomplicated Angular promise following the invocation of a service

Delving into the realm of Angular promises for the first time, I'm determined to grasp its concepts. In my MainController, I have a simple authentication process using myAuthSrv.authUser with a username and password. Upon successful authentication (. ...

Advantages of opting for bin files instead of .js files with express-generator

Starting a Node.js project with Express typically involves using express-generator. Once the project is created, your file structure will resemble this: . ├── app.js ├── bin │ └── www ├── package.json ├── public │ ├ ...

Turn off the autofill option for passwords in Google Chrome

Is there a way to prevent the password autocomplete dropdown from appearing when clicking on the password field? In Chrome Version 61.0.3163.100, none of the solutions seem to be working. Screenshot This issue has been raised many times, but setting autoc ...

Uploading CouchDB document attachments using an HTML form and jQuery functionality

I am currently in the process of developing a web form that, upon submission, will generate a couchdb document and attach file(s) to it. I have followed tutorials and visited forums that suggest a two-stage process similar to futon's approach. While I ...

Adding JSON data to an array for Flot Diagram

I've been struggling with the task of organizing data into an array. The existing temp array is structured as follows: tmp[0][0] = "NY" tmp[0][1] = "52" tmp[1][0] = "FL" tmp[1][1] = "25" My goal is to transfer this data into a new array called data. ...