角度访问模块范围从工厂
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
};
}]);
相关文章:
- 正在全局范围中查找JavaScript函数
- 如何通过数组更新角度子范围
- 如何使用ngrepeat和双向绑定获得指令的隔离范围
- Angularjs工厂注入错误
- 如何将参数传递到angularJs中的工厂
- HTML范围:动态设置值属性
- 为什么只有工厂在棱角分明的网站上被提及
- "实例范围”;TypeScript类的getter/setter
- jquery日期选择器年份范围默认值
- AngularJS:$q.dedefe()不能由工厂方法共享
- Jpgraph:如何手动设置X轴和Y轴的范围
- 在对象数组中查找多个值的d3范围
- Angular:从工厂更新范围不起作用
- 角度工厂,对象范围混乱
- 角度访问模块范围从工厂
- 从父控制器传递工厂数据以隔离范围指令
- 努力将结果从工厂返回到范围
- $interval范围引用角度工厂内的方法
- Sencha touch 2外部工厂范围
- AngularJS$范围在一个工厂