gpt4 book ai didi

AngularJS - 自引用服务?

转载 作者:行者123 更新时间:2023-12-01 08:08:00 25 4
gpt4 key购买 nike

我正在构建一个 Angular 应用程序,它将有一个顶级 Controller 和一个二级 Controller 。将有 n 个二级 Controller ,但我想将全局级功能放在某个地方。我在服务中这样做。

我正在开始创建一个返回 api 的单个服务的路径,实际上,包含许多函数(如下)。该服务正在返回一个具有两个属性分支的对象,每个分支都包含一组函数。我怎样才能从另一个中调用其中一个?

globalModule.factory('global', function($http) {

var squares = MyApp.squares; // this is the *only* link from Global namespace to this app

return {
squareMgr: {
getSquaresEarned: function() {
return squares.earned;
},
getSquaresPlaced: function() {
return squares.placed;
},
setThisSquareEarned: function(value) {
squares.earned.push(value);
},
setThisSquarePlaced: function(value) {
squares.placed.push(value);
}
},
missionMgr: {
missionInfo: {},
setMissionInfo: function(missionInfo) {
this.missionInfo = missionInfo
},
complete: function(missionData) {

log('complete called on video at ' + new Date());
missionData.complete = true;
log(angular.toJson(missionData));

$http({
url: '/show/completeMission',
method: "POST",
data: missionData
})
.then(function(response) {
if (response.data.success === true) {

log('completeMission success');

// increment squares earned counter
this.squareMgr.setThisSquareEarned(missionData.id);

// above is an attempt to run a function contained in this
// same service in a different parent property branch.
// how *should* I do this?
}
});
}
}
}
});

最佳答案

这样的事情怎么样:

globalModule.factory('global', function($http) {

var glob = {
squareMgr: {
// ...
},
missionMgr: {
foo: function() {
glob.squareMgr.xyz();
}

}
};

return glob;
});

关于AngularJS - 自引用服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20982164/

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