Hey Luke,
it's simple. The first argument to $watch function can be only
FUNCTION OR STRING.
If it's a string, it is parsed (see $parse) into a function. (To be
precise, even numbers/objects are parsed, but to noop function).
Then, angular calls this function and if result of the current call
does not equal previous one, the second argument (function) is called.
So, strings are evaluated as ANGULAR EXPRESSION.
Angular expressions:
'123' -> 123
'true' -> true
'false' -> false
'"some"' -> "some"
'prop' -> will be looked up on current scope
Now, back to watching attributes.
<div something="property"> -> $attr.something === 'property'
scope.$watch($attr.something, function(){});
You are passing a string "property", treated as angular expression ->
will be looked up at scope.property.
<div something="123"> -> $attr.something === '123'
scope.$watch($attr.something, function(){});
You are passing a string "123", treated as angular expression -> will
be number 123 (so it will never change, the watcher will get called
only once)
Anyway, watching attributes only makes sense, if you interpolate them,
eg. <div something="prop-{{id}}">, then, you want to use $observe:
$attr.$observe('something', function() {});
Looking into your fiddle - none of these watchers makes sense to me.
If you want to get "vertical" value, just read $attr.direction, why
would you watch it ? It can't change, unless you interpolate it (use
double curlies). If you want to get proper types (say boolean/number)
instead of strings, just evaluate it once scope.$eval($attr.count),
but again, no reason for watch, as it will never change the value.
Do you have any idea, how to make it simpler ?
V.
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/angular/-/B6J0aohnXq4J.
> To post to this group, send email to
ang...@googlegroups.com.
> To unsubscribe from this group, send email to
>
angular+u...@googlegroups.com.
> For more options, visit this group at
>
http://groups.google.com/group/angular?hl=en.