Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding value to input in Angular JS

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'; 
like image 922
mTuran Avatar asked Sep 14 '12 09:09

mTuran


People also ask

What is Ngvalue used for?

The ng-value directive sets the value attribute of a input element, or a select element.

What is #value in angular?

The AngularJS ng-value directive is used to set the value attribute of an input element, or a select element. It is mainly used on <radio> and <option> elements to set the bound values when these elements are selected. It is supported by <input> and <select> elements.

Which directive binds the value of application data to HTML input controls in AngularJS?

ng-model directive binds the values of AngularJS application data to HTML input controls.

What is $scope in AngularJS?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).


2 Answers

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/

like image 187
Dan Caragea Avatar answered Oct 11 '22 07:10

Dan Caragea


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/

like image 28
Frank6 Avatar answered Oct 11 '22 08:10

Frank6