When switching from $scope to "controller as" syntax, I run into trouble with the "this" reference. I need to call to a service to get some data for the initial page load up. But the "this" I get refers to the global window object rather than an instance of the exercise controller. Kind of weird. Here's what I know.
The AngularJS inclusion in my main page...
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
Here's how the controller gets included into the template page.
<div class="col-xs-9"
ng-include="'exercise/exercise_body.html'"
ng-show="mainController.showStatus.exercises"
ng-controller="ExerciseController as ec"
ng-init="ec.initialize()"></div>
Here is the initialize function from the ExerciseController.
this.initialize = function(){
// Item lists to display on different parts of the page
this.dataList = {};
this.dataList.exercises = [];
this.dataList.resources = [];
var promise = exerciseService.getExercises();
promise.then(function(res){
console.log(this); // this is a window object? why????
this.dataList.exercises = res.data.exercises;
});
}
And lastly, there is the stack trace from Chrome....
TypeError: Cannot set property 'exercises' of undefined
at app.js:82
at angular.js:13189
at l.$eval (angular.js:14401)
at l.$digest (angular.js:14217)
at l.$apply (angular.js:14506)
at l (angular.js:9659)
at S (angular.js:9849)
at XMLHttpRequest.D.onload (angular.js:9790)
So what that leaves me with is a datalist object that's undefined even after setting a definition explicitly. Any ideas on what can be done to fix this or work around this issue?