In AngularJS there are two styles of writing controllers, the "the controller as syntax" and the "'attach to $scope' style of controller" (both quotes from the ngController documentation.) There are several questions on StackOverflow comparing these styles, for example this vs $scope in AngularJS controllers and Performance differences between controller functions defined on $scope
or this
- AngularJS.
I have a method on a controller which needs to prompt AngularJS after a model update. Using the $scope style of controller I can do that thus:
myApp.controller('MainController', ['$scope', function($scope) {
$scope.content = "[Waiting for File]";
$scope.showFileContent = function(fileContent) {
$scope.content = fileContent;
$scope.$apply();
};
}]);
But if I write the controller using 'this'
myApp.controller('MainController', function () {
this.content = "[Waiting for File]";
this.showFileContent = function(fileContent){
this.content = fileContent;
};
});
how do I invoke $apply()?