Good evening,
I am writing an application using AngularJS and I require for the application to send data with a POST request to the nodejs server.
My data is structured like as a json object and it has data binding thanks to the AngularJS framework.
As of now, a function is dynamically trying to create possible values that the user might like inside of some input tags. An example:
<button ng-click="generateFoodAndBeverages(row)>Generate</button>
<input type="text" ng-model="row.service.day.beverage" placeholder="beverage" />
<input type="text" ng-model="row.service.day.food" placeholder="food" />
The two input values can be set by the user by typing in the value they would like (e.g. "Cola", "Hamburger"), but above the input tags is a button that can generate the input values for the user.
The function that generates the values takes them from an array and then at the end of the function returns two possible values, one for beverages and one for food.
When it has the two returning values it changes the attribute value of both inputs, setting them to the two possibilities generated by the function:
jQuery("#input1").attr("value", generateFoodAndBeverages(row)[0]);
jQuery("#input2").attr("value", generateFoodAndBeverages(row)[1]);
This is not perfect nor elegant but it's working. The function populates and dynamically changes the value attribute of those two input elements each time the user requests for automatic generation of food and beverages so the values are actually set and do exist.
Even so, even if I see them on screen as text inside the input fields, my POST request does not recognize the fact that the ng-model actually changed. The only way the ng-model registers the changes to the value attribute of the input fields is if the user types something with his keyboard, manually changing the value attribute. Another example:
<input type="text" value="generateValue()" ng-model="row.service.info" />
The one up here does not change the ng-model value at all.
<input type="text" value="User Typed Value" ng-model="row.service.info" />
This other one instead does change the ng-model value and as it changes and exists, it is passed to the $scope that can later be sent as a POST request to the server.
Any ideas as to why the "automatically and dynamically generated" value of the input field does not get registered by the ng-model while the user typed value does?
Thanks in advance!
[EDIT]
Apparently the problem comes with the ng-model not changing. I tried to debug the problem by applying an ng-change in the input. If the change is done by javascript, it is not registered with the ng-model and the ng-change function does not fire because the ng-model was not changed even tho' I can clearly see the new value set by javascript for the input tag. If I change the value of the input tag by hand the ng-change is fired and the console logs the change.
I could apply the changes directly to the ng-model if it weren't so different for each row.
Having the ng-model like this:
<input ng-model="row.serviceInfo.DayObject[dayString].food" />
<input ng-model="row.serviceInfo.DayObject[dayString].beverage" />
How would I be able to apply the changes directly to the ng-model given how dynamic the model is. As an example, I could have 1000 rows, each with their own serviceInfo object. I don't know how I could change the model for each of those rows with the dynamically generated values.
[EDIT]
The problem was indeed with ng-model not changing. The solution consisted in applying the changes to the ng-model for each element inside the dynamically generated values function. Thanks everyone for the input. I'll leave this piece of code here if anyone ever comes across the same problem! Thanks again!
let foodEl = angular.element(the row element food input);
let beverageEl = angular.element(the row element beverage input);
$scope.displayedCollection[i].serviceInfo = {
"day" : {
"food" : generatedValuesFood(el, day),
"beverage" : generatedValuesBeverage(el, day)
}
};
foodEl.val($scope.displayedCollection[i].serviceInfo.day.food);
beverageEl.val($scope.displayedCollection[i].serviceInfo.day.beverage);
I think that your problem is quite simple. As #ssougnez said, don't mixed jquery with angularjs. Angularjs use data-binding concept, don't use jquery style to change the input value instead use the ng-model directive to bind data from the model to the view on HTML controls (input, select, textarea). In your generateFoodAndBeverages function just set the ng-model value according to which row for eg:
var generateFoodAndBeverages = function () {
$scope.row.service.day.beverage = array[0];
$scope.row.service.day.food = array[1];
};
Related
I set the input value Array property sum and it shows value in input but when submitting form does not get Quantity property in Order object. If I change the value then I get Quantity property value. How can I get model value from ng-value?
<input ng-model="Order.Quantity" ng-value="subOrderList.sum('Quantity')" type="number">
To answer your question, "ng-value does not update Ng-model", it is by design. As mentioned in comments, ng-value and ng-model are not intended to by used in this way.
It isn't entirely clear what you are trying to achieve here, so here's a couple potential solutions:
If you are just looking to display a value then you don't need to use an input at all. Both of these will behave the same and update when needed:
<span>{{subOrderList.sum('Quantity')}}</span>
<span ng-bind="subOrderList.sum('Quantity')"></span>
If you actually need this value to be updated by user input then the HTML would look like this:
<input ng-model="Order.Quantity" type="number">
And then you will need to manually update that value in a controller or service when needed:
Order.Quantity = subOrderList.sum('Quantity');
From your comments it almost seems like you need an input that also changes dynamically and sporadically, but without a data example or more code I can't really see how that would work.
I am setting values on a form in an iframe via Javascript.
Please note that I do not have access to the page displayed in the iframe. My Javascript page is on the same server, so it has access to the form displayed.
//HTML of Forename field in form control
<input class="form-control" id="Forename" type="text" data-bind="value: dto.Forename">
Javascript setting the value:
var frameNode = document.getElementById('frm1');
var fieldNode = frameNode.contentDocument.getElementById('Forename');
fieldNode.value = FirstName; //previously defined
The values set successfully (see attached img). However, when I hit SAVE, I still get a 'values Required' message. I suspect this is because the Knockout Javascript libraries that binds the value with the view model, needs a keypress.
Even when I manually go into the form and press Enter/Tab after each value, I still get the message. It's only when I change the Forename and Surname manually to something else that the Save is successful.
Has anybody done something like this before? Thanks
In this image you can see the values are set
I believe the problem you're experiencing is actually due to a deeper issue involved in using a knockout binding. Updating the value of a UI control directly has no effect because the real underlying value is stored in a javascript view-model object. The DOM element only mirrors that value, and updates are performed using a change event hook under the hood. When you change the value manually on the UI the change event is triggered and the view-model value gets updated.
Knockout.js Docs
So to properly update the values you should try using the knockout library to update the underlying data:
var frameNode = document.getElementById('frm1');
var fieldNode = frameNode.contentDocument.getElementById('Forename');
var viewModel = ko.dataFor(fieldNode);
viewModel.dto.Forename(FirstName); //value setting works like a function
If you can't get that to work you can also try manually triggering the change event. That's far easier if you have jQuery, but can still be done without. See How can I trigger an onchange event manually?
I tried to update input element's value using .val() and it turned out that it doesn't affect the value="" attribute. After reading some topics here on stackoverflow, it turned out that these are not the same.
I started changing them both to be sure I do not do any mistake there:
$("#elementid").val(variableNumber).attr('value', variableNumber);
Now, lets say that I am sending the value of my input to validate it in php.
Which of these will be sent ff I make them different?
$("elementid").val(variable1);
$("elementid").attr('value', variable2);
Is there any rule on this? or any factor that makes one of these being sent as the actual "value" ?
Which of these will be sent ff I make them different?
The input's current value will be sent. The current value is reflected by the the value property. The value attribute represents the default value of the input, not its current value (and is reflected as the defaultValue property on the input). The default value is used to initialize the value when the input is created, and to reset it if you use the reset method of a form it's in.
Unless you want to change the default value, there's no need to set the value attribute, just the property. The property is what val changes, what changes when the user acts on the input, and what gets sent when the form is submitted.
When you are working with <input type='text'> I think you should use attr('value', variableNumber); instead of val(value).
val(value) is useful when working on a jQuery object containing elements like <input type="checkbox">, <input type="radio">, and <option>s inside of a <select>. More infomation here.
I've written a function which gets called when input textbox text changes -
On HTML -
<input id="unique" type="text" data-ng-change="KeywordChange(filterKey)" ng-model="$parent.filterKey">
in Controller
$scope.KeywordChange = function (filterKey) {
//some logic goes here
}
Keyword change function works well when input text box text changes. but I want this function NOT to be called when I change value of input text box like this
$('#unique').val('');
$('#unique').change();
and I've to change textbox value programatically - so is there any option to know keywordChange function is being called by actual text change in textBox or called by programatic call to $('#unique').change();
If you want to change the value of the textbox (that is bound to some data) programmatically in AngularJs, then all you need to do is update the corresponding Model.
Using jQuery to update the UI is most definitely not recommended.
You'd need to just update the property "filterKey" in JavaScript from within your AngularJs code.
Why not just use ng-keyup since all you care about are physical key strokes?
I have such HTML:
<form data-bind="submit: mySubmit>
<input type="text" ...
And I want to access the input's value upon submit:
mySubmit = function() {
var textValue = ???;
alert(textValue);
}
How can I do that ? I am OK with giving a kind of ID to the text field, but I don't want this ID to be global (for example I may have several of these forms on one page).
If you are looking at it from a Knockout perspective, then you really want to have the value of your input represented in your view model. This would mean adding a data-bind="value: myValue" to your input. Then, you would access it from the view model in your mySubmit method.
Something like: http://jsfiddle.net/rniemeyer/sAyET
I would not recommend it, but the submit method is actually passed the form element in its first argument by Knockout (it really should be passed the current data and event, but currently it is the element).
So, you could do something like: http://jsfiddle.net/rniemeyer/sAyET/1/. Ideally, your view model should not have any references to the DOM/view in it, so I would not recommend this option, unless absolutely necessary.