I am trying to rewrite a JS/jQuery SPA using AngularJS. The SPA is an item editing grid using the amazing Handsontable. I was delighted to see that there is an AngularJS version in the works, and that, for the most part, it works just as expected.
However, I'm having a hard time figuring out how to do something I am doing in jQuery in AngularJS. The grid has (among others) 3 columns, one of which is dependent on the other two. The three columns are cost, selling price and margin ( which is (1 - (cost / price)) * 100 ). I can do the math in the controller and pass the margins into the $scope.items variable for the initial loading, but it obviously does not update when you change the cost or price columns. It seems like the perfect place to do two-way data binding, but I can't figure out how to bind the datacolumn value of margin to the value of two other rows. I feel like there is an obvious way to do this, but since I'm new to AngularJS, I thought I might throw it out to the community.
Thanks in advance!
Edit:
So after playing around with some of the answers, I want to refine my question a bit to get to the heart of the matter. I have a REST API serving a JSON document that contains arrays of items from an online point of sale service. Here is a sample with dummy data:
{ '@attributes': {'count':10}, 'Item': [
{ 'customSku':'sampleItem1'
, 'description':'Sample Item, first'
, 'defaultCost':'10.00'
, 'Prices': {'ItemPrice':[
{'amount':'17.99', 'useType':'Default'}
, {'amount':'19.99', 'useType':'MSRP'}
]}
}
, { 'customSku':'sampleItem2'
, 'description':'Item, Sample, 2nd'
, 'defaultCost':'20.00'
, 'Prices': {'ItemPrice':[
{'amount':'29.99', 'useType':'Default'}
, {'amount':'39.99', 'useType':'MSRP'}
]}
}
]}
I'm not joking that the data is structured like this, it totally is. Anyway.
Right now, I am passing this to the view in my controller like this: $scope.items = JSON.parse(request.responseText);
. How do I pass the function: $scope.getMargin = function() { return (1 - (cost / price)) * 100 }
to the view in such a way that cost and price are scoped to the correct item? I feel like I am really close to getting it, but I haven't quite gotten there. Thanks to the answerers so far!
Edit 2:
Plunker here, as requested!