I have an input field with a number in it. I want to make it possible to edit the field and update it. So it is like an input field with the number 5 and when i change it to 6 it has to automatically update it self. So if i refresh the page the number should have been updated to the number 6. I've tried it with ng-change but it didnt work.. The code that i used was:
<input type="text" ng-change='change(di)' value="{{di.hours}}">
To test the ng-change i used the following code in my Controller
$scope.change = function(data){
alert(data);
};
The problem was, it didn't alert anything when i had changed my input.
Try using ng-model to bind input to some property of scope.
To detect changes of scope you can use $watch function.
Try using ng-model="{{di.hours}}" instead to bind input, you will get value automatically.
And do not use ng-change
you can do $watch in angular controller like
scope.$watch('di.hours', function(newVal, oldVal) {
alert(newVal);
});
Related
I am using Laravel with Angular JS for forms. I got a text box in the form as below
<input type="text" name="removedSiblings" ng-model="form.removedSiblings"
id="div_removedSiblings" class="form-control">
Using Javascript I am assigning a value (a button click)
var elem = document.getElementById('div_removedSiblings');
elem.value = 'malai';
Got the below code inside the Controller PHP
$removedSiblings = \Input::get('removedSiblings');
logger($removedSiblings);
I always get null value for $removedSiblings variable whenever the field gets value using the javascript.
But, when I type a value manually inside the text box, then that value is printed inside the controller.
Whats wrong here?
The AngularJS framework does not watch the value property of <input> elements. It only updates the model after user input or an update from inside the framework.
Use the ng-click directive for the button:
<button ng-click="form.removedSiblings = 'malai'">
Click me
</button>
For more information, see
AngularJS ng-click Directive API Reference
It's is because of the usage of ng-model which uses two way binding.. Try to set the value on the controller scope variable which is form.removedSiblings.
hope this helps.
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];
};
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 a checkbox in my view
<input type="checkbox" ng-model="trackers[tracker._id].enable" ng-click="toggleTracker(tracker._id);"/>
And a function in my controller
$scope.toggleTracker = function(trackerId){
$scope.trackers[trackerId].enable = !$scope.trackers[trackerId].enable;
console.log($scope.trackers[trackerId].enable)
}
But each time I check the box the enable property does not change. What could be wrong with my code?
Since you have ng-model connected to the input it should change when you click. Then you also have the ng-click so it changes back to the first value.
Just remove the ng-click and your good.
You shouldn't use both ng-model and an ng-click function toggling the enable state. Maybe it overrides and negate the toggle because they are executed one after the other.
Just use the ng-model property and it should work.
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.