问题
Documentation for a change in $compile when upgrading from AngularJs 1.5 to 1.6 states:
pre-assigning bindings on component/directive controller instances is disabled by default, which means that they will no longer be available inside the constructors.
— AngularJS Developer Guide - Migrating to V1.6 - $compile
The upgrade example in the documentation is as follows (shortened):
Before
.component('myComponent', {
bindings: {value: '<'},
controller: function() {
//...
}
})
After
.component('myComponent', {
bindings: {value: '<'},
controller: function() {
this.$onInit = function() {
// ...
};
}
})
I already discovered that I have to use the same $onInit function for any directive using bindToController: true like here:
.directive('acAllocation', acAllocation);
function acAllocation(SomeService) {
return {
restrict: 'E',
replace: true,
scope: {
allocation: '=acAllocation'
},
controller: acAllocationController,
controllerAs: 'vm',
bindToController: true,
templateUrl: 'path/acAllocation.html'
};
function acAllocationController() {
var vm = this;
this.$onInit = function () { //...
Are there any other types of bindings which are affected by this change?
Or is it enough to deal with components and directives with bindToController:true?
Rephrasing the same question: In an Angular 1.7 application only using directives with bindToController: false: can I face any issues regarding pre-assigning bindings at all?
回答1:
The bindings are complete when $onInit() lifecycle method is called. This is the only guarantee. It's no longer safe to assume that values are available in the constructor, and this affects the entire application.
I would recommend moving to 1.5 style components and ES6 in order to make migration easier in the future.
来源:https://stackoverflow.com/questions/51274406/upgrade-angularjs-1-5-to-1-6-which-exact-bindings-are-affected-by-change-in-c