1

I have an input field as

<input type="text"  ng-model="sampleValue" >

In some situations i am clearing the value of input as

$scope.sampleValue = "";

Is there any way to detect the change in value of input field from code?

In this case ng-change is not working as the value is changing from code. I have a number of such input fields, so using multiple $watch is not a good solution.

3
  • 1
    why not use $watch? You can put all those values in single object any add watch to it. Commented Dec 14, 2017 at 5:11
  • You can create a wrapper component and listen to $onChanges of it. Commented Dec 14, 2017 at 5:11
  • Like a form?@31piy Commented Dec 14, 2017 at 5:11

2 Answers 2

1

You may try $watchGroup instead of multiple $watch:

var fieldList = ['sample1', 'sample2', 'sample3'];

$scope.$watchGroup(filedList, function(newVal, oldVal) {
  console.log(newVal[0], newVal[1], newVal[2]);
);
Sign up to request clarification or add additional context in comments.

Comments

0

if possible make use of rxJs and Observable value , like in below example it observe string s, in you code it can observe $scope.sampleValue = "";

  let s = "Hello World";
  Observable.of(s).subscribe(val => {
    console.log(val);
  });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.