Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-change not working on a text input

I am new to angular js. In my code there is color picker initialized from a text field. User changes the value of color and I want that color to be reflected as a background of a text in a span. It is not working. What is missing?

HTML:

<body ng-app="">    <input type="button" value="set color" ng-click="myStyle={color:'red'}">    <input type="button" value="clear" ng-click="myStyle={}">    <input type="text" name="abc" class="color" ng-change="myStyle={color:'green'}">    <br/>    <span ng-style="myStyle">Sample Text</span>    <pre>myStyle={{myStyle}}</pre> </body> 

Plunker - http://plnkr.co/edit/APrl9Y98Em0d6rxuzRDE?p=preview

However when I change it to ng-click it works.

like image 793
Ashwin Avatar asked Sep 30 '14 07:09

Ashwin


People also ask

How do you use NG on change?

The ng-change event is triggered at every change in the value. It will not wait until all changes are made, or when the input field loses focus. The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript.

How does ng disabled work?

Definition and Usage The ng-disabled directive sets the disabled attribute of a form field (input, select, or textarea). The form field will be disabled if the expression inside the ng-disabled attribute returns true. The ng-disabled directive is necessary to be able to shift the value between true and false .

What is Ng Onchange?

ngOnChanges(): "A lifecycle hook that is called when any data-bound property of a directive changes. Define an ngOnChanges() method to handle the changes." We use this lifecycle hook to respond to changes to our @Input() variables.

How do ng options work?

Definition and Usage. The ng-options directive fills a <select> element with <options>. The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when using the ng-options directive.


Video Answer


1 Answers

ng-change requires ng-model,

<input type="text" name="abc" class="color" ng-model="someName" ng-change="myStyle={color:'green'}"> 
like image 172
Petr Averyanov Avatar answered Oct 04 '22 06:10

Petr Averyanov