I have input like this
<input type="text" name="widget.title" ng-model="widget.title" value="{{widget.title}}"/>
I want to change input value dynamically so i use that but it doesn't change the value:
$scope.widget.title = 'a';
You don't need to set the value at all. ng-model takes care of it all:
set the input value from the model
update the model value when you change the input
update the input value when you change the model from js
Here's the fiddle for this: http://jsfiddle.net/terebentina/9mFpp/
If you don't wan't to use ng-model there is ng-value you can try.
Here's the fiddle for this: http://jsfiddle.net/Rg9sG/1/
Use ng-value for set value of input box after clicking on a button:
"input type="email" class="form-control" id="email2" ng-value="myForm.email2" placeholder="Email"
and
Set Value as:
$scope.myForm.email2 = $scope.names[0].success;
Some times there are problems with funtion/features that do not interact with the DOM
try to change the value sharply and then assign the $scope
document.getElementById ("textWidget") value = "<NewVal>";
$scope.widget.title = "<NewVal>";
{{widget.title}}
Try this it will work
Related
In an input text field I have to take the value and use it in my .ts file.
can you suggest me which of the following syntax should be used for this?
If you want synchronous alignement between what you see in text field and what you have in the variable use
<input type="text" [(ngModel)]="num">
If you want to show in text input field the value of num property and align num property with the value of text field at certain events (e.g. change) then use
<input type="text" [value]="num" (change)="onNameChange($event)">
I hope this helps
I'm doing a input with ng-model. But I want to keep the original value when someone put invalid number inside the input box. How can I achieve this?
Here's the plunker:
http://plnkr.co/edit/wX7n0jBn1Ek1py4DJHqT?p=preview
In the input box I'm using ng-model to bind the value. Also I specified the type of the input to be number.
<input type="number" name="input" ng-model="example.value" min="0" max="99" required>
But when I change the input the ng-model also changes. Can I delay this ng-model change until the number is valid? Or can I use ng-change to achieve this? But it seems that the ng-model will change when you change your input so the ng-change cannot capture the original value.
To make things clear, I'll have an example here:
if the current ng-model is 10, And I type 50. It'll change to 50. Then I type 5000 and it'll change to 5000.
If the current ng-model is 10, and I type 'aaa', It'll go back to 10.
The part I want to ask is how to make a copy of ng-model as I can set the ng-model back when I'm using onchange in this input box.
Putting you input type number you already validating your model correctly, what you need is to check if your form is valid. Take a look at validation with Angular here.
To make your value change only after input loose focus, use ng-model-options, your input should be like this:
<input type="number" name="input" ng-model="example.value" min="0" max="99" required ng-model-options="{ updateOn: 'blur' }">
You can always make directive to store last valid value, just add parser:
ngModelCtrl.$parsers.push(function(val) {
if (val) {
stored = val;
console.log(stored);
}
return val;
});
Then when you want you can revert value to last valid one. Example plunk:
http://plnkr.co/edit/XCacrP3Ij4ZCgyYYaoIa?p=preview
I don't know what you want to achieve with it, it's better to disable the save button until the form is valid.
return back to old value will create more problem, say current value is 10, I changed to 50, means valid and changed the value, further I typed to make it 5000, which value you want to return, 10, 5 or 50?
$scope.$watch('example.value', function(newValue, oldValue) {
if(newValue != oldValue){
//validate
if(!valid)
$scope.example.value = oldValue;
}
});
I am using angularjs and I have an input like
<input type='text' pattern="[0-9.,]+" ng-model="treasuryValue" />
I set first value to input using ng-model tag. But while setting first value to input my pattern doesn't work. Pattern tag runs first time when input changes. How can I format value in input while setting it's first value.
Use ng-pattern attribute,
<input type='text' ng-pattern="/[0-9.,]+/" ng-model="treasuryValue" />
To format the value initially,
function formCtrl($scope,$filter){
$scope.treasuryValue = '33,345';
$scope.$watch('treasuryValue', function() {
$scope.name = $filter('number')($scope.name);
});
}
Demo
I would like manage with AngularJS UI which allows to pick one of the options (displayed as radio buttons group) or a custom value typed in a input-text.
It should look like:
http://jsbin.com/foyujali/7/edit
Here is the code that you can see also in the link below:
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.js"></script>
<div ng-app="tagsApp" ng-controller="TagsCtrl">
<input type="radio" id="conversion_type_sale" ng-model="tag.conversion_type" value="sale"/>
<label for=conversion_type_sale>Sale</label>
<input type="radio" id="conversion_type_lead" ng-model="tag.conversion_type" value="lead"/>
<label for=conversion_type_lead>Lead</label>
<input type="radio" id="conversion_type_custom" ng-model="tag.conversion_type" value="{{tag.conversion_type_custom_value}}"/>
<input type="text" placeholder="Custom" ng-model="tag.conversion_type_custom_value" id="conversion_type_custom_value"/>
<p>
The choosen conversion type is: <strong>{{tag.conversion_type}}</strong>
</p>
</div>
And JS:
angular.module('tagsApp', []).
controller('TagsCtrl', function ($scope) {
$scope.tag = {conversion_type: 'lead'};
});
I would prefer not to use ngChange directive so I just bind the value or ng-value (I tried both) to the model of the input-text. It doesn't work properly this way, but I suppose there is an elegant AngularJS solution. Any suggestions?
Thanks in advance!
P.S. Just to clarify - I want the following functionality: http://jsbin.com/foyujali/10/edit but I want to avoid using ngChange directive.
This is what you're looking for: http://jsfiddle.net/colares/shcv8e1h/2/
Explaning the behavior
By focusing on the text field, the radio on the left is selected and chosen value is updated regarding the value of the text field;
By focusing on radio on the left of the text field, the chosen value is updated according to what is in the text field as well;
By changing the value of the text field, the chosen value is also updated;
Finally, by focusing on any other label or radio, the custom value remains, while the chosen value is update regarding the selected option.
To do so, I had to use a few more things in custom option input:
ng-focus: when I click on the text field, it updates the chosen value regarding the text field;
ng-change: as I update the text field, the final value is also updated;
ng-model: to store the auxiliar variable customColor, which remains regardless of the selected value.
Remember, the ng-value is used to set the value of the radio (or an <option>) from a given expression when we select it. This makes the radio and input text "bound", because they have the same value.
You could use $scope.$watch and look for the change in your controller like so:
http://jsfiddle.net/2R6aN/
var app = angular.module('tagsApp',[]);
app.controller('TagsCtrl', function ($scope) {
$scope.tag = {conversion_type: 'lead'};
$scope.$watch('conversion_type_custom_value',function(new_val) {
if (new_val) {
$scope.tag.conversion_type = new_val;
}
});
});
$watch is best option. Also, Instead of using modelName in 1st parameter of $watch, you can create your own stuff(eg. watching length of input box) and do desired action on it with second parameter.
how can i change the value of this input field using jquery?
<input type="hidden" name="emailaddress" value="admin#admin.com">
$('input:hidden[name=emailaddress]').val('something');
Here,
input:hidden point to hidden input
[name=emailaddress] check for name attribute.
so totally
$('input:hidden[name=emailaddress]') select the input which is hidden and with name attribute emailaddress.
.val() used to set/get value to input field. With parameter it acts as a setter and getter without it.
you need to give the input an id and then reference the id and change it. below is what i have used to change the value of a radio button. this uses JS
document.getElementById('ID').value = somevalue;
Along with
<input type="hidden" name="emailaddress" id ='ID'value="admin#admin.com">