gpt4 book ai didi

javascript - Angular——如何使资源全局化?

转载 作者:行者123 更新时间:2023-11-29 16:59:08 25 4
gpt4 key购买 nike

我目前正在成功地将音轨资源加载到我网站的“作品”页面。以下是我的“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com