[英]angular access module scope from factory
我对棱角还很陌生。
我想要的是,当从外部调用工厂方法时,该方法应更新模块作用域数据,如下所示:
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);
}
};
}]);
但是它说,$ scope是一个未知的提供程序。 还有其他方法可以更新模块数据吗? setFilesForDevice
是一种通过单击其他控制器模板内的按钮来调用的方法。
您需要在这里采取一些不同的方法。 首先,通过$ routeParams.device在控制器中获取设备ID。
然后,创建一个可注入FileListController的服务,并提供有关文件的信息,即
fileList.controller('FileListController', ['$scope', '$routeParams', 'deviceFilesService', function ($scope, $routeParams, deviceFilesService) {
$scope.device = $routeParams.device;
$scope.files = deviceFilesService.getFilesForDevice($routeParams.device);
}]);
fileList.service('deviceFilesService', ['files', function (files) {
this.getFilesForDevice = function (device) {
// Code to look up list of files the the device
};
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.