-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Isolate scope properties not bound to controller #15619
Comments
It is because of this documented breaking change. The best approach is to use the |
I'm not quite sure that adding some function to the controller is more elegant than scope properties be bound to the controller before it gets called. Not saying it's bad, just... Odd. Do you have (a link to) a more detailed explanation of the reasoning behind this decision? Because I honestly don't see the advantage of having a discrete magic function. |
It is not a magic function. It is a lifecycle hook 😃 Lifecycle hooks enable developers to hook into certain points in the lifecycle of a component (initialization, removal, input updates etc) and perform relevant operations easily. You can read more about it in the Dev Guide. The Angular 1 hooks are modelled as close as reasonable after Angular 2+ hooks, so it is easy to migrate. We implemented them because they turned out to be useful - they are not directly related to deprecating bindings pre-assignment. (Indeed the There are several drawbacks for pre-assignment, including (but not limited to):
You can find relevant discussions in #14580 (starting at #14580 (comment)) and #14206. |
Thanks for explaining. It's clearer to me now. However,
|
I didn't say it is hard. I said it is more complex than it needs to be. Even the oversimplified version... var instance = Object.create(Constructor.prototype);
assignProperties(instance);
Constructor.apply(instance, args); ...is more complex than... var instance = new Constructor(...args) On top of that, there are other subtleties that need to be taken into account. Very briefly:
It pretty much does (now with bindings pre-asignment disabled, it does it even better) 😃
.component('classyComponent', {
template: `
<div ng-style="{color: $ctrl.color}">
I am {{ $ctrl.thatMuch }} classy!
</div>`,
bindings: {
color: '@'
},
controller: class ClassComponentController {
$onInit() {
this.thatMuch = 'very';
}
}
})
Not sure I follow. Anywhere you can use a constructor function, you can use a class.
No framework can do funky stuff that the browser does not support on its own. What you can do (and this is independent of frameworks, libraries etc) is use some kind of tool to transpile from a different dialect or language (e.g. ES2015, ES2016, ES2017, TypeScript) to a version of JavaScript that is supported by your target browser (typically ES5 nowadays). You can do this with Angular (2+), you can do this with AngularJS (1.x), you can do this with no framework at all. Both Angular and AngularJS allow you to write applications in ES5, ES2015 (ES6), TypeScript etc.
The issues I linked to in my previous comment have relevant discussions.
It may be fine for some people, but it is not fine for others 😃 |
You make a few fair points. It's okay to have differing opinions about this stuff of course, and I'm not sure we should be using this ticket much further to discuss how ng1 is supposed to work. I trust it's been thought out well. I did learn a bunch though, so thank you very much for taking the time to explain things to me. I love learning :) I hope I didn't seem too defensive towards you - that's just my rusty view on the world. |
I hope I didn't come across as too defensive (or offensive) either 😃 I was just trying to give my (personal and not necessarily correct) views on every point you raised. I, too, think differing opinions are great. In any case, thanks for raising your concerns. It is mostly the users' needs and feedback that drive the evolution of AngularJS. |
Do you want to request a feature or report a bug?
Bug.
What is the current behavior?
The properties set in an isolated scope object hash for a directive, are not bound to the controller, even when using
bindToControler: true
. They only appear to be bound to the template.Here's a demo: https://jsfiddle.net/z9pgasbt/
It will console.log
undefined
for a value calledname
that has been bound via isolate scope to the controller. All documentation and tutorials point to it should work this way.What is the expected behavior?
The above example should print
"Foobar"
instead ofundefined
. Technically speaking, the properties defined in an isolate scope should be bound to the controller, and should be usable as such in the controller - not just in the template.What is the motivation / use case for changing the behavior?
Using bindToController with controllerAs as it appears to be intended.
Which versions of Angular, and which browser / OS are affected by this issue? Did this work in previous versions of Angular? Please also test with the latest stable and snapshot (https://code.angularjs.org/snapshot/) versions.
Firefox 50, AngularJS 1.6.1.
Tested with snapshot version - problem persists.
The text was updated successfully, but these errors were encountered: