我想創建一個負責生成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);
});
我想參考播放器數組裏面的添加和刪除方法,但我回來了未定義。