Avoid "$apply() already in progress" error

Check `scope.$$phase` before calling `$apply()` to avoid errors.