Check if in a $digest cycle before calling functions through $apply.

I've added checks in the two places where $apply is used. You could also use a factory to do this check and invoke the function but IMO it would be overkill for the code. The minified version was updated as well which would bring it up to master.