Hi Guys,
I have a view that is displaying a table, and I would like to perform
certain calculations at "render time" for want of a better description. Id
like to compute things like the differences between values in 2 columns as
the data is rendered into the view, with the additional requirement of
storing the result of the computation as a field in the scope object.
My first attempt was to create a filter to do this - it worked but I
couldn't get it to create a new field in the scope object - I beed this so
that I can do things later on like applying a sort.
My second attempt was to create a directive, which carries out the
computation and adds the result into the scope object. This works, however,
I'm wondering if this is a good "testable" solution since Im not
encapsulating UI in the directive, just some JIT calculations.
Any suggestions?
here's an example of such a directive:
.directive('difference',[function(){
return {
restrict: 'A',
link: function(scope, element, attrs) {
scope.$watch('asset.newvalue', function(val){
if(scope.asset.oldvalue !== undefined){
scope.asset.diff = scope.asset.oldvalue - scope.asset.newvalue;
}
});
}
};
}])
Just to clarify, I could perform this in the view as something like
{{asset.oldvalue - asset.newvalue}} BUT the result of this expression is
not added to the scope.
any suggestions much appreciated,
thanks,
Martin
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.