Currently, I am in the process of developing an Angular 1.5 directive and have encountered a frustrating issue related to manipulating data that is not yet available.
Below is a snippet of my code:
app.component('formSelector', {
bindings: {
forms: '='
},
controller: function(FormSvc) {
var ctrl = this
this.favorites = []
FormSvc.GetFavorites()
.then(function(results) {
ctrl.favorites = results
for (var i = 0; i < ctrl.favorites.length; i++) {
for (var j = 0; j < ctrl.forms.length; j++) {
if (ctrl.favorites[i].id == ctrl.newForms[j].id) ctrl.forms[j].favorite = true
}
}
})
}
...
Essentially, I am fetching favorites via AJAX and then comparing them to the list of forms that are bound within the directive.
The issue arises from the fact that the promise is resolved before the binding is actually populated... resulting in 'ctrl.forms' still being undefined when the loop is executed!
Is there a way to overcome this hurdle without resorting to using $scope.$watch, which contradicts the 1.5 component approach?