angularjs directive not upating with scope.$watch -


i trying focus input based on controller variable. initializes correctly printing value= false in console , variable being udpated in view controller not in directive. im using jade/coffeescript or id create example pretty it:

controller

$scope.autofocus = false  $scope.somefunction = ->   $scope.autofocus = true 

directive.

'use strict'  angular.module('myapp') .directive 'gsautofocus', ($timeout, $parse) ->     {     link: (scope, element, attrs) ->         model = $parse(attrs.gsautofocus)         scope.$watch model, (value) ->             console.log 'value=', value             if value == true                 $timeout ->                     element[0].focus()                     return             return     } 

view

input(type="text", gsautofocus="{{autofocus}}") 

you should use $observe on attribute you want evaluate interpolated contain on each digest, $watch not work on interpolated value.

$observe

$observe(key, fn);

observes interpolated attribute. observer function invoked once during next $digest following compilation. observer invoked whenever interpolated value changes.

code

'use strict'  angular.module('myapp') .directive 'gsautofocus', ($timeout) ->     {     link: (scope, element, attrs) ->         attrs.$observe 'gsautofocus', (value) ->             console.log 'value=', value             if value == true                 $timeout ->                     element[0].focus()                     return             return     } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -