I'm pretty new to angular.
What I want is that when a factory method is called from outside, the method should update the modules scope data, like this:
fileList.controller('FileListController', ['$scope', function ($scope) {
$scope.device = {};
$scope.files = [];
$scope.isDeviceDefined = function () {
return typeof $scope.device === 'object' && $scope.device !== null && $scope.device.hasOwnProperty('label');
};
}]);
fileList.factory('deviceFiles', ['$scope', 'files', function ($scope, files) {
return {
setFilesForDevice: function (device) {
$scope.device = device;
$scope.files = files.getFilesFromDevice(device.label);
}
};
}]);
But it says, that the $scope is a unknown provider. Is there a other way, that the modules data can be updated? setFilesForDevice is a method that is called by clicking a button inside a different controllers template.
$scopetechnically is local to the controller, i.e. there is no Angular service providing it; the view subsystem provides it to each controller it instantiates. So no, services cannot have the scope injected to them. And they shouldn't, conceptually it is wrong to have the service layer depending on the view. What can you do? (1) have the service return an object and bind that to the scope (2) pass the scope to the service method as argument [(3) possibly others]. I would go for (1).