I am currently refactoring some code in an angular 1.5.8 application to use components.
(Very similar to some steps in this guide: https://teropa.info/blog/2015/10/18/refactoring-angular-apps-to-components.html) The basic cases are working correctly.
But I get a problem when I need to have my component both updates a variable and also call a function (both bound from parent). In this case, the function call seems to happen before the variable is bound. So when the method executes on the parent it is still using the old contents of the variable.
How can I make sure the variable is updated before the method executes?
See comment in the code below, it's the two lines of the reset() function.
angular.module('searchfieldComponent', [])
.component('searchfieldComponent', {
templateUrl: "/js/common/components/searchfield.component.tpl.html",
bindings: {
labelText: '@',
searchText: '=',
searchCallback: '&'
},
controllerAs: "vm",
controller: [function() {
var vm = this;
vm.search = function() {
vm.searchCallback();
}
vm.reset = function() {
vm.searchText = null;
// When the method bound to searchCallback executes in the parent,
// the variable bound to searchText has not yet been set to null
// it is still the old value.
vm.searchCallback();
}
}]
});