我想创建一个负责生成PlayerList的工厂,但是我在访问我在初始化函数中设置的变量时遇到问题。代码是参考在角度工厂中初始化变量
app.factory("PlayerList", function(){
// Define the PlayerList function
var PlayerList = function() {
this.initialize = function() {
// create an array for our players
var players = [];
};
this.add = function(player) {
this.players.push(player);
}
this.remove = function(player) {
if (players.length > 0)
{
this.players.splice(players.indexOf(player), 1);
}
}
this.initialize();
};
return (PlayerList);
});
我想参考播放器数组里面的添加和删除方法,但我回来了未定义。