New to Angular - Some questions

Showing 1-3 of 3 messages
New to Angular - Some questions Jason Maggard 6/18/13 8:45 AM
Hello guys,

I am moving to Angular from Knockout, and I have a few issues. I'm assuming that I must be doing something a non-angular type of way.

Here is a very basic fiddle that outlines two of my major problems...

Problem 1: val1 and val2 are initialized as 3 and 4, and add up to 7 properly. However, if you change either of the values in the text boxes, the new value is treated as a string and I get concatenation instead of addition. Change val1 to 4 and you get 44 when it should be 8. What is the best way around this behaviour?

Problem 2: Calculated fields. I can get a calculated field by using the curly brackets like {{val1 + val2}} and have the calculated fields auto update when the underlying model changes, but this is totally unacceptable. In my full fledged app, we generate a "cost" that is used several times throughout and having to put in the cost calculation each and every time is a pain. Not to mention that when this calculation changes, I now have the unenviable task of finding 15 different places that use the cost calculation and updating them all.

In addition, if I try to put an ng-model="cost" on the input with the curly brackets, then the curly brackets don't work. So nothing jumps out at me as a way to bind cost.

This example is more like the structure I desire. However, unlike a ko.observable, the calculated fields do not update when the values that generate them change. The boilerplate solution everyone has foisted on me is to write a bunch of ng-change handlers... But that is awful. If width changes change the cost and change the payback calculations, etc... It quickly becomes a tangled mess.

Both of these methods fail as far as separating logic from presentation. Method one has my business logic embedded in my HTML. Method two puts a whole bunch of ng-change handlers in my code which isn't that much different from having to write a whole mess of onChange handlers in plain ol' HTML. If I HAVE to do a bunch of ng-change handlers, I would just as soon do an onChange handler in Javascript because I can at least declare them outside of my presentation layer.

Here's a knockout version of the same:

This is more like what I would expect... Nothing but data-binds on my inputs, all program logic nicely contained within the view model. Also, since my computable is a Javascript function, I don't have to scratch my head about how to ensure my two values are numeric.


Computed variables: Is there a way to watch the underlying variables and update the computed amount automatically? Without having to bury my program logic in my HTML?
Is there a good way to keep my numbers from turning into strings?

Thank you for your help.
Re: New to Angular - Some questions Jason Cust 6/18/13 9:08 AM
#1: You could simply use the number type for the input field:
  If you can't use the number type, you can do something like: or

#2: Why not just set a $scope.calculatedCost and then similar to the second example fiddles above and have it set that variable whenever a change occurs to any of the underlying values.
Re: New to Angular - Some questions Jason Maggard 6/18/13 10:11 AM
Changing user inputs to overcome a programming problem is usually a last resort.

However, the second answer works. The $scope.$watch solves the problem of fields auto-updating and allows me to work with my variables somewhere I have control.