I am new to AngularJS, I successfully designed the repeated forms using nested ng-repeat. But I can't find the right way to get all values corresponding to the forms.
Here is my updated JSFiddle: http://jsfiddle.net/MrSuS/gTc5v/7/
Update: And you can see, radio input is also not working properly.
there is no binding in your application use ng-model to bind your field values.
if you want to design custom form depends on your dynamic data you should write a directive like this Example Form Directive
UPDATE
I think best way is repeat html form in your position.
It is simple, easy and less code...
<div ng-repeat="form in forms">
<h2>{{form.name}}</h2>
<input type="text" ng-model="form.answer.city"><br/>
...
here is JSFIDDLE example...
Firstly use ng-model for binding. Second, use $index as array subscript for your ng-model
ng-model="input[$index]"
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.
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];
};
In a controller, we can sometimes access inputs values by the value of their DOM id instead of setting an ng-model directive and then binding the DOM value to $scope.
For example, in
<input type="text" ng-model="foo" id=foo>
we can either use $scope.foo or foo.value in the controller. What is the advantage of using $scope in this case?
I think that the main benefits of using ng-model instead of getting the value of input by id is two-way binding. Your variable from ng-model is always up to date and you can use it directly in html or wherever you want.
<input type="text" ng-model="foo" id="fooInput" />
<p>ng-model value: <span ng-bind="foo"></span></p>
If you choose approach when you get value from input by id you will lose this feature, but you get a little bit better performance. Because whenever you type into into, it will trigger a $digest cycle causing Angular to update all watchers and bindings in the app to see if anything has changed.
A little demo on plunker.
Is there any performance improvement in using ng-change in an input instead of ng-model?
I pressume that when ng-model is used in an input, then a "$watch" (or similar) is made by angular in a variable, and that adds work load.
But if ng-change is used, then the variable (model) can be updated when needed, code can be executed only when this variable is changed by the input.
This works assuming that only one input can change a variable.
Example follows:
using ng-model
<input type="text" ng-model="ElTexto">
<div ng-show="ElTexto"></div>
using ng-change
html
<input type="text" ng-change="elTexto()">
<div ng-show="ElTexto"></div>
js
$scope.elTexto(){
$scope.ElTexto = true;
}
ng-change requires ng-model, so you can't choose between the two. You must use ng-model, and can also use ng-change if you wish.
Be aware that the two are VERY different. ng-model will keep your input's value and its backing model in perfect sync; ng-change will indicate that the user is interacting with the input. If you care about the value they're changing, like if you're doing autocomplete, just use ng-model and have all your code share the same variable. If you specifically want to take an action when the input changes, regardless of what the value is, then you can use ng-change for that.
ng-change will have batter performance then ng-model. As in every digest cycle ng-model will be evaluated while ng-change will be evaluated on change of input.
I have an AngularJS page with several form inputs.
When the some of the inputs have focus, I want to change other, arbitrary, aspects of the page.
For example, when the user is in the 'stock code' input, I want to display the list of popular stock codes. When they are in the 'qty' field I want to show in-stock quantities and lead times.
Is there a variable which contains the 'current' input (the one which has focus), or do I need to revert to jQuery's onFocus. (It seems a little primitive now.)
How about make a directive? You could set a variable whenever the user leaves/enters the input, and detect that: http://jsfiddle.net/TZnj2/
Be sure to read the directive guide if you're confused as to what's happening: http://docs.angularjs.org/guide/directive
Also I use scope.$apply in that directive, here's an explanation of what that does: http://docs.angularjs.org/api/ng.$rootScope.Scope#$apply
ngFocus and ngBlur are built-in directives:
<input ng-focus="hasFocus = true" ng-blur="hasFocus = false" type="text">
<p ng-show="hasFocus">The field has focus!!</p>
Try demo on jsfiddle