-
Notifications
You must be signed in to change notification settings - Fork 27.3k
$scope.$watch listener expressions can access $newVal & $oldVal #1708
Conversation
a string listener expression can evaluate $newVal and $oldVal
example:
```javascript
$scope.$watch('foo', 'foo = $newVal');
```
|
@flazz what is the motivation behind this change? If I understand correctly it just makes it easier for people to write JavaScript functions as strings (expressions). I've got my private opinion on this but before sharing it I would love to hear more about your use-case here. |
|
So there is the question of whether it is a good idea to allow strings as listeners at all! |
|
My biggest practical use case (to date) is sample above. Also the case where the I like @peterbacondarwin's reasoning, I feel without access to old and new there is a half feature. How much value does the full feature provide?. Also is |
|
@flazz I'm not sure I understand your example (please bear with me...). Are you talking about this one:
If so, it seems to be assigning a new value value to a variable... that its changing its value to a new one anyway (this is why the watcher is executing in the first place). I'm sure that I'm missing something here so if you could provide a practical use-case it would be awesome! |
|
This is effectively a no-op! I could imagine calling a function... Also, I think that there should also be a unit test where $oldVal was not undefined. |
|
@petebacondarwin Wouldn't your second example already be taken care of by the $watch function? $scope.$watch('foo', saveModel);Where |
|
@petebacondarwin This is exactly my point, the first example does nothing... unless I'm mistaken. While I could also imagine this call
There must be something I'm missing here... |
|
To some extent I agree, hence my thought that string based listeners could be dropped altogether. But in defence of my example, you may be passed a string for some reason - perhaps as an attribute on a directive. |
|
@petebacondarwin the below case illustrates a non-noop case This could easily be written with the listener as a function. Is there a use to string expression listeners beyond a shorthand sugar? |
|
@flazz for me it just moves to string something that could be written as a normal function. So it would be -1 from me but I might be missing a good use-case here... @IgorMinar could you have a quick look and have a final word here? |
|
-1 we should not be encouraging people to put code into strings within javascript files. closing... |
|
Then should we not deprecate the use of strings as listeners altogether? On 8 January 2013 21:53, Igor Minar notifications@github.com wrote:
|
a string listener expression can evaluate $newVal and $oldVal
example: