Mr Smith
Mr Smith

Reputation: 3486

Set a $watch on a <SELECT> element in an Angular directive?

How do I set a $watch on the selectedvalue property of a SELECT element in an angular directive? What is the syntax? I have a partial directive below. It doesn't blow up, but the watch never fires.

      link: function (scope, elem, attr, ngModel) {


        scope.$watch(elem.context.selectedOptions, function (selectedType) {
          console.log("nodup choice");
        })
      }

Upvotes: 1

Views: 510

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

I don't see a need here to put a watcher over an select dropdown to fire something on its value change. I'd suggest you to use ng-change event over that select box & have function over it. So that will get fire whenever input value gets changed.

<select ng-model="selectedValue" ng-change="changeFn()">
  <option value="1"></option>
  <option value="2"></option>
  <option value="3"></option>
</select>

Upvotes: 2

Related Questions