角度访问模块范围从工厂

时间:2015-01-22 12:18:15

标签: javascript angularjs angularjs-scope

我对角度很新。

我想要的是,当从外部调用工厂方法时,该方法应该更新模块范围数据,如下所示:

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是通过单击不同控制器模板内的按钮来调用的方法。

1 个答案:

答案 0 :(得分:1)

你需要采取一些不同的方法。首先,您通过$ 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
    };
}]);
相关问题