-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Controller prototype / instance methods #321
Comments
whoa... this is crazy... I guess it makes sense when you think about how method dispatch and
A solution to this problem would be using I think that binding proto methods makes sense, because that's the behavior you expect not the crazy inherited instance method behavior. |
We've been playing with the idea of injecting scope into controllers instead of binding it. I think that would solve this problem as well:
|
I hope the first code example is easy to understand for everyone who knows, how JS works. Injecting scope would solve it as well, as it's almost the same approach (instead of using var self = this, you get the variable self(scope) as a parameter) Actually, I like the idea "injecting the scope" quit a bit. I believe it would solve the problem with testing controllers without scope ! You can inject easily whatever you want then ! (It would make a sense to set the I haven't thought about injecting scope enough, this is really quick response, so maybe I'm missing something, but I will think about it more, it looks promising. Not because of this problem, but because of testing controllers. |
So injecting scope, is a long term solution, which looks attractive, but we need to think about all of the implications. We have to bind the methods on the prototype, since we have to copy them to scope. The binding is optional, but the copying is a must. I think that we could make the parser smarter, which would fix this problem. Currently the parser just dispatches the method as it finds it, but it could do extra work for functions, and dispatch them on the instance which they are defined, and I think that would solve the issue. Here is where the function call is applied: https://github.com/angular/angular.js/blob/master/src/Scope.js#L76 |
It's funny, but I've found this "feature" useful sometimes - if you know, how it works, you can take advantage of it... |
no, its very confusing, especially to people who are new to JS. |
There is a different
this
(inside controller method - different scope object), if method is defined in instance than if defined in prototype. And the method is invoked from child scope element (i.e. element created by repeater).At least we should mention that in doc, as it's bit confusing.
There is a gist with example: https://gist.github.com/907914
Reason
Prototype methods are binded, so that they are always called with root (for given controller) scope as
this
. See https://github.com/angular/angular.js/blob/master/src/Scope.js#L583Instance methods (defined in constructor) are only applied, so that they are called with related scope as
this
. (that's child scope inside repeater)Possible solution
Unify the API - do we need to bind the prototype methods ??? I hope we don't.
User has to access parent scope through $parent or $root property.
The text was updated successfully, but these errors were encountered: