And just when you think your day couldn't get any worse after the Bowie thing, here I find a creepily weird thing in every directive used as input fields templates in the project I'm currently helping to bugfix.
The bug was that some textarea models weren't bound to their variables in a controller.
I work my way into a 6700 lines controller and even though it was written really bad (you can't even begin to imagine how many different $scopes can be defined in a 6700 lines controller!), it didn't seem to have any issue related to the bug.
So I go check the directive. Template was fine. Function was... well, I don't know.
I'll just leave it here for your amusement (and sorry for the non-Angular crowd, who won't understand what's going on), just stripped down of the non-pertinent things.
First, the directive gets included like this:
Code:
<textarea-field model="notes"></textarea-field>
Which trasforms into:
Code:
<textarea ng-model="textareaField" ng-change="change()"></textarea>
You may have noticed that the directive attribute is called "model", but the actual ng-model is "textareaField".
And there is also a function that gets triggered on change.
And this is why:
Code:
app.directive('textareaField', function() {
return {
scope : {
model : '=model'
},
link : function ($scope, $element, $attrs) {
$scope.$watch('model', function() {
if ($scope.model != undefined)
$scope.textareaField = $scope.model;
});
$scope.change = function() {
$scope.model = $scope.textareaField;
}
}
};
});
Now I don't know the bug is triggered by the directive link (but I replaced the directive with a simple textarea and it works properly).
But... what the heck happens there?
Is it like a clunky attempt to rewrite Angular's 2-way data binding?
I mean: it watches the original model which, at every change, updates a new scope variables which, at every change, updates the original model.
Am I reading it right?
(also funny: renaming the scope.model as scope.model
)