I have the following
Rails HAML:
= select_tag "some-class",
options_for_select([['None', '']], ''),
{ class: 'some-other-class',
'ng-model' => 'someModel',
'ng-options' => 'option.name for option in someList',
'ng-change' => 'updateSelected()'}
Angular Controller:
scope.updateSelected = ->
#logic for updating model lives here. Model updates successfully by using some values defined within scope. Includes the following:
scope.someModel = "some_new_value"
Angular Directive:
SomeClassDirective= ->
restrict: 'C'
link: (scope, element, attrs) ->
monitorFormFields = (newValue, oldValue) ->
console.log "this is the inner function call"
#logic for setting the inner _destroy field lives here
scope.$watch 'someModel', monitorFormFields
However, when the Select List value is changed, ‘this is the inner function call’ never prints.(it does print when the directive first initializes, ie at page load). My question therefore is: Why isn’t the $watch expression triggering, and how do I get it to trigger?
Thanks!
With this HTML:
Here is a directive that will watch for a change to
someModel:Controller:
Note that you don’t need to call a controller method to update
someModel— Angular does that automatically for us because of theng-modelattribute. So, the directive only needs to $watch for a change to that $scope property.Fiddle.
A more Angular approach would be to have model properties control whether “0” or “1” is displayed. E.g., in your controller:
In your HTML:
In monitorFormFields() you can change the values of these scope properties, and the view will automatically update — there is no need to “find” siblings or update .val()ues.