I'm currently going through a transition from plain JS to Angular. The main reason I'm trying to achieve this is to keep my original library code intact as much as possible; saving the need for me to add each function to the controller.

Any ideas on how I could go about achieving this? Comments on the above fiddle are also welcome.

@dk123 angular.element("#scope") is not working, though angular.element($("#scope")) is working, you need to have jquery also
– Arun P JohnyMar 15 '13 at 5:11

1

I know it been a while, but I hope some can answer me on this ... Why does var scope = angular.element($("#outer")).scope(); have to be declared inside the change function? If I move it to the global space it's a no go?
– Marc M.Nov 6 '13 at 9:47

1

@MarcM. I think it has to do with Angular's scope recreation. By the time you're using the change function, the previous scope the global var was pointing to may no longer exist (due to the recreation).
– dk123Dec 14 '13 at 3:43

The above code basically creates a function called safeApply that calles the $apply function (as stated in Arun's answer) if and only Angular currently isn't going through the $digest stage. On the other hand, if Angular is currently digesting things, it will just execute the function as it is, since that will be enough to signal to Angular to make the changes.

Numerous errors occur when trying to use the $apply function while AngularJs is currently in its $digest stage. The safeApply code above is a safe wrapper to prevent such errors.

(note: I personally like to chuck in safeApply as a function of $rootScope for convenience purposes)

Why does your safeApply function work? Seems like what you are saying is "execute the function by itself if Angular is in the $apply or $digest stages, otherwise use $apply() to apply the function".... But if you execute the function by itself.... how does that update any models? Seems like that would not be favorable behavior, unless there something going on I don't know about. Does some mechanism in Angular go an poll the $scope for changes that may have happened directly to it???
– trusktrMar 26 '14 at 5:37

Plus, if you need to safeguard against those states, then I'd consider that a bug of the $apply() method that needs to be fixed.
– trusktrMar 26 '14 at 5:37

@trusktr From what I understand, executing the function normally is caught by angular if the function changes any models, and hence angular updates them in the next digest stage.
– dk123Mar 27 '14 at 0:54

@trusktr I'd agree though that if the normal $apply() can be applied without the safeguards, there would be nothing better. In essence, the only purpose of safeApply is to safeguard against the $apply() errors. Not sure though if this was a reported issue and now fixed, or still an ongoing one.
– dk123Mar 27 '14 at 0:56

I still don't understand why this comment isn't the best answer for this. After digging the internet for couple of days, with frustration and anger, finally this is what solved my issue. Thank you @Charleston. You are great, sir!
– RajkumarAug 24 '16 at 4:44

The accepted answer is great. I wanted to look at what happens to the Angular scope in the context of ng-repeat. The thing is, Angular will create a sub-scope for each repeated item. When calling into a method defined on the original $scope, that retains its original value (due to javascript closure). However, the this refers the calling scope/object. This works out well, so long as you're clear on when $scope and this are the same and when they are different. hth

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).