Assign a value to ng-model using JavaScript

Encountering an issue while working with JavaScript in AngularJS. There is a text field identified by id="longitude".

<input type="text" data-ng-model="newwarehouse.longtitude" id="longitude"/>

The value of this field is being set using JavaScript.

var longitudeValue = document.getElementById('longitude');
longitudeValue.value = event.latLng.lng();

However, the Angular JS controller is unable to retrieve the value of this field unless it is manually typed in. How can I adjust my JavaScript to properly set the model value for this field? Thank you for any insight provided.

Answer №1

When using angular, it's best to follow the angular way of setting or updating values.

If you need to update an input field based on a specific event, such as a button click, here's how you can do it:

Let's say you have the following HTML code:

<a href="javascript:void(0);" ng-click="clickme()">Click me</a>

Make sure to update your controller with the necessary click event binding:

 $scope.clickme = function() {
     $scope.newwarehouse = {
         longtitude : 'longitute'
     };
  };

For further reference, I've created a plunker for you: http://plnkr.co/edit/F1eXNVKsmYsn5TMQxxH6?p=preview

Answer №2

To update the input text with a value, simply define your model in the controller

$scope.newWarehouse = {
    longitude: 456
}

Answer №3

In order for Angular to register the changed value, it is important to invoke the input event.

For more detailed information on how to trigger an "input" event programmatically without using jQuery, please refer to this answer: How do I programmatically trigger an “input” event without jQuery?

I encountered a similar scenario due to my use of a pure JS library, which necessitated this approach.

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

Is the RouterModule exclusively necessary for route declarations?

The Angular Material Documentation center's component-category-list imports the RouterModule, yet it does not define any routes or reexport the RouterModule. Is there a necessity for importing the RouterModule in this scenario? ...

Tips for limiting a website to load exclusively within an iframe

I am managing two different websites: https://exampleiframe.com (a third-party website), https://example.com (my own website) My goal is to limit the loading of https://example.com so that it only opens within an iframe on https://exampleiframe.com To a ...

Is it possible for two-way binding to function in index.html within Angular 4?

Does two-way binding function in index.html? I have some links and metadata in index.html. How can we define head parameters in a component? <head> <meta name="og:title" content={{titleValue}}> <meta name="og:url" content={{urlValue}}> & ...

The radio button element could not be located using the attribute "checked="Checked""

While working with Geb, I encountered an issue using the code div.find("input","checked":contains("checked")). This is the snippet of code I attempted to use in order to locate the checked radio button on a webpage. However, I received an error when using ...

How can we update the form builder or form group in Angular 2 when making changes to the existing data in a table? I'm a bit confused on how to implement router

<tr *ngFor="let row of categories "> <td>{{row.categoryName}}</td> <td>{{row.visible}}</td> <td>{{row.instanceNumber}}</td> <td> <a class="btn btn-info btn-fill " [routerLink]="['/con ...

The HTML elements in my JSX code seem to constantly shift around whenever I resize my webpage

Using react.js, I'm currently developing a website that appears like this before resizing: pre-resize_screenshot_website However, upon vertical or diagonal resizing, the layout becomes distorted especially in the 'availability search bar' ...

While running tests on a React project, the `npm test` command is successful, but unfortunately,

I created a new react app using create-react-app and included some basic components and tests. The tests work fine when running 'npm test', but I encounter an 'Unexpected token' error when using Jest to run the tests with imported compo ...

Combining multer, CSRF protection, and express-validator in a Node.js environment

Within my node.js application, I am utilizing an ejs form that consists of text input fields in need of validation by express-validator, an image file managed by multer, and a hidden input housing a CSRF token (a token present in all other forms within the ...

What is the reasoning behind placing CDN links at the bottom of the index file?

What is the reason for placing CDN links for AngularJS file at the end of the index page? I initially placed it at the top of the file and it worked fine. Is there a significant difference between these two placements? ...

Ways to effectively apply multiple filters in an ng-repeat directive

A functioning repeater is currently set up to display fields on their respective pages. <div ng-repeat="p in model.pages"> <div ng-repeat="f in model.fields | filter: { pageNumber: p.pageNumber }:true"> <div class="pdf-field"> ...

Is there a way to align these side by side?

Is it a silly question? Perhaps. But I can't seem to figure out how to align my text and colorpicker on the same line instead of two. Take a look at my fiddle. I've tried removing display:block and clear:both, but that didn't do the trick. H ...

adjusting three sections within a flexible navigation bar

Having difficulties creating a dynamic navbar that changes on scroll and keeping the elements within the nav fixed when the menu options appear. For reference, you can check out this codepen: http://codepen.io/timothyfernandez/pen/azXQPV Any assistance w ...

Having trouble getting Emberjs to properly handle an Ajax POST request

Currently, my goal is to send data to the database using Ajax POST. To start off, I have an HTML form as follows: <script type="text/x-handlebars" id="project"> <div class="row"> <div class="span6"> <form class ...

Tips for applying ng-class to elements in AngularJS that need to be styled both conditionally

What is the best way to achieve success in this task? ng-class="{'a':x, 'b':::y}" I am attempting to apply 'y' only once and not bind it with 'x'. I have also experimented with using multiple ng-class directives: ...

Utilizing the variables defined in the create function within the update function of Phaser 3

I'm facing an issue in my game where I can't access a variable that I declared in the create function when trying to use it in the update function. Here is a snippet of what I'm trying to achieve: create() { const map = this.make. ...

Is it beneficial to display three.js on a <canvas> rather than a <div>?

I have come across examples in three.js that use: renderer = new THREE.WebGLRenderer( { canvas: document.querySelector( 'canvas' ) } ); This relates to a <canvas></canvas> element. On the contrary, there is another method: rendere ...

Is there a way to retrieve a raw value from the API response that matches the one shown in POSTMAN, but my attempts have been unsuccessful?

Looking for some assistance with fetching data from a front-end (React) to the raw value in JSON. Specifically, I'm having trouble with the login functionality. When I input my email and password, the response should match what I see in POSTMAN, but i ...

"JQuery's selector is failing to locate elements once they have been loaded through an

I am facing an issue where jQuery selectors are not working on elements loaded from the server via Ajax requests, but they work fine in normal mode. $('#myid').change(function(){ alert('OK!'); }); <select id="myid"> <optio ...

Disable the ng-click event when swiping left with ng-swipe-left

The button functions flawlessly on a touchscreen when clicked or swiped to the left: <button ng-click="click(it)" ng-swipe-left="leftSwipe(it)" /> However, on a desktop where left-swiping is done by combining a mouse click with a drag to the left ...

"Troubleshooting Problem with JSON Encoding in PHP and Parsing with getJSON in

Apologies if this sounds like yet another discussion on the topic, but I've been struggling for hours without finding a solution. I'm attempting to retrieve data from a MySQL database, generate a JSON using PHP, and then parse this JSON in JavaS ...