What is the best way to manipulate an input field in a controller if the input is not connected to an ng-model?

Received some backend-rendered HTML code.

Here is the HTML code:

<form  name="mk" ng-submit="submit()">
    First name:<br>
    <input type="text" name="firstname" value="Mickey">
    <br>
    Last name:<br>
    <input type="text" name="lastname" value="Mouse">
    <br><br>
    <input type="submit" value="Submit">
</form>

Need to access and modify the firstname and lastname fields in the controller. Seeking a solution within AngularJS without using JavaScript or jQuery. Any suggestions?

Answer №1

To accomplish this task, you can store the name within the scope:

JavaScript:

$scope.user = {};
$scope.user.firstName = "John";
$scope.user.lastName = "Doe";

HTML:

<form  name="jd" ng-submit="submit()">
    First Name:<br>
    <input type="text" name="firstname" value="{{user.firstName}}">
    <br>
    Last Name:<br>
    <input type="text" name="lastname" value="{{user.lastName}}">
    <br><br>
    <input type="submit" value="Submit">
</form>

Example:

http://plnkr.co/edit/CsVsbYPU8hExLfGQ4NGW?p=preview

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

Discovering the parameters at your disposal within a callback function can be easily achieved in JavaScript and MongoDB

Being new to JavaScript and web development, I've been self-studying but have encountered many roadblocks. Currently, I'm struggling with understanding functions with callback functions and their parameters. The documentation I've read so f ...

The continuation of unraveling the mystery of utilizing `overflow-y:scroll` in a horizontal slider

As a beginner, I realized too late that adding code in comments is not an option. Consequently, I decided to create a new question and ask for your assistance once again. To optimize the use of space, I have implemented bxSlider with fixed dimensions (wid ...

React: Import default export as a string

Help needed with JSON data import import dataOption1 from './Option1.json' import dataOption2 from './Option2.json' async setParamsByDomain(optionUser) { await this.setState({ jsonName: "data"+ optionUser}); console.log(t ...

Ways to resolve the issue of the experimental syntax 'jsx' not being enabled

I encountered an issue while trying to implement react-fancybox and received an error. https://i.sstatic.net/vgFha.png To resolve the error, I executed the following commands: npm install --save-dev @babel/preset-react, npm install --save-dev @babel/plugi ...

The limit of $digest() iterations in AngularJS 10 has been reached, creating difficulty in maintaining synchronization between the service, controller, and view by utilizing $watch, which is leading

I've identified the issue at hand. I am using a $watch, and due to the value being set each time (by invoking a function in my service), a new array is constantly created, triggering the watch to detect a new value repeatedly. In light of this, my mai ...

The minDate feature in Gravity Forms is failing to disable dates in the past

I’m attempting to prevent users from selecting past dates on the datepicker within Gravity Forms, but I've encountered issues such as console errors or the functionality not working properly... Initially, I attempted the following approach: gform.a ...

Having trouble targeting multiple dropdowns using jQuery

I am encountering an issue with two separate dropdowns where the hover states cannot be targeted by CSS based on the HTML markup. After attempting to solve this using jQuery, I have made some progress but one problem persists. Whenever I hover over the lin ...

Deactivating choices in Autoselect from Material UI

I am attempting to implement a feature in the autocomplete of material ui where options are disabled based on a specific condition. Each table row contains an autocomplete field, and when an option is selected in one row, it should be disabled in the next ...

Having trouble sending Props between components within a specific route as I keep receiving undefined values

Here is the code for the initial component where I am sending props: const DeveloperCard = ({dev}) => { return ( <Link to={{pathname:`/dev/${dev._id}`, devProps:{dev:dev}}}> <Button variant="primary">Learn More</Butt ...

What is the best method to determine if a Django object is null in JavaScript?

Is there a way to verify if a Django object is null in JavaScript? At this moment, I have a variable called person which can hold a Django object or null. In my JavaScript code, I am using: if ({{person}} != null) { execute_function({{person}}) } Ca ...

Vue error: Uncaught promise rejection - RangeError: The computed value update has exceeded the maximum call stack size

My computed code snippet: computed: { display: { get() { return this.display }, set(newValue) { this.display = newValue } } }, Attempting to update the computed value from a function in ...

A selection dropdown within a grid layout

Hey there, I am currently working on implementing a dropdown list within a data grid. I want to ensure that if I select a value in one row of the dropdown and then select the same value in another row, a message will be displayed saying 'already added ...

Updating $scope from another controller in AngularJS

I am facing an issue where I need to update the $scope inside a directive's link function. Here is what my controller and directive look like: $scope.current = 0; angular.module('myAPP') .directive('post', function() { ...

What is the process of utilizing a file for $template cache?

I am trying to incorporate a template from an HTML file. To do so, I included a script with the id pointing to my HTML file. <script type="text/ng-template" id="app/directives/userAvatarTooltip.html"><script> and attempted to retrieve the te ...

Shifting divs to different positions upon clicking

I am currently working on a project where I have three divs in a container positioned next to each other. My goal is to make them change their positions when clicked. For example, clicking on the left div should move it to the center position. Here is my p ...

What is the best way to deploy a Vue Webpack application using Sails.js?

I am currently managing a Sails.js server alongside a Vue Webpack application, running as separate entities. Here's how my folder structure is laid out: /application-root/server/ /application-root/client/ Within the /server/ directory lies my Sails ...

Exploring click event beyond React Component: Jest + Enzyme Testing

I am looking to create a Jest and Enzyme test for a component that includes an event listener. The purpose of the test is to ensure that when a mousedown event occurs outside of a specific HTML element, a change is triggered (for example, toggling a state ...

Interacting with local data using Express server

I am currently in the process of developing a web application for my web art class using Node.js with npm and Express. The concept behind the site is to have the entire body colored in one solid color, but allow users to text a hexcode/CSS color to a Twili ...

Print the information obtained from a synchronous AJAX request in the console

I'm struggling to wrap my head around this issue. I am attempting to save the data from a synchronous ajax call in a variable and then log it to the console. However, the final console log is executing before the synchronous call has completed, so it ...

How can I incorporate multiple states into a conditional statement in ReactJS?

Is there a more efficient way to achieve this task? I'm struggling to come up with a cleaner solution as the current approach looks messy. I need to check multiple states in an if statement and the only method I can think of right now is the one prese ...