我目前正在成功地将音频轨道资源加载到网站的“作品”页面上。这是我的“Works” Controller 如何实现这一点的:

'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中使用工厂来实现此目标吗?如果可以,如何实现?

最佳答案

'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;
  });

关于javascript - Angular –如何使资源全局化?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29724300/

10-09 21:58