Angular - 如何使资源全球化?

时间:2015-04-19 00:07:04

标签: javascript angularjs angularjs-scope

我目前正在将音轨资源成功加载到我网站的“作品”页面。以下是我的“Works”控制器实现此目的的方法:

'use strict';

angular.module('appName')
  .controller('WorksCtrl', function ($scope, $http, socket) {
    $scope.works = [];

    $http.get('/api/works').success(function(works) {
      $scope.works = works;
      socket.syncUpdates('work', $scope.works);
    });
  });

但是,现在我需要在整个网站上全局访问我的曲目。我可以通过我的中心app.js中的工厂来实现这一点,如果是这样,怎么做?

1 个答案:

答案 0 :(得分:5)

'use strict';

angular.module('appName')
  .controller('WorksCtrl', function ($scope, WorksFactory) {
    $scope.WorksFactory = WorksFactory; //can refer to works in the view from WorksFactory.works
  });
  .factory('WorksFactory', function ($http, socket) {
    var fact = { works: []};

    $http.get('/api/works').success(function(works) {
      angular.copy(works, fact.works);
      socket.syncUpdates('work', fact.works);
    });
    return fact;
  });