当其他人随机更换时,如何让所选项目保持不变?角

时间:2015-05-20 18:43:35

标签: javascript angularjs

我使用ng-repeat指令显示数组中歌曲的名称:

<table class="table">
    <tr ng-repeat="song in ctrl.newSongs">
        <td>{{song}}</td>
    </tr>
</table>

此处的号码更改:

<input class="numInput" min="1" max="songs.length" required ng-model="ctrl.number" ng-change="ctrl.changeNumber()">

嗯,这是我的整个js代码。

function Controller(CommonVariables) {
    this.number = CommonVariables.getNumber();
    this.clicked = 0;
    this.newSongs = [];
    this.localHistory = [];
    this.marked = [];
    this.changeNumber = function() {                                                                                                                                                
        CommonVariables.setNumber(this.number);
    };

    this.songEnding = function(number) {
        if (number == 1) {
            return 'song';
        }
        else {
            return 'songs';
        }
    };

    this.isInHist = function(songName) {
        this.histCheck = 0;
        for (i=0;i<history.length;i++) {
            if (history[i].indexOf(songName) > -1) {
                this.histCheck = true;
                break;
            }
        }
        return this.histCheck;
    };

    this.getRandom = function(max) {
        return Math.floor(Math.random() * (max + 1));
    };

    this.addLocalHistory = function(newSongs) {
        this.localHistory = [];
        for (i=0;i<newSongs.length;i++) {
            this.localHistory[i] = newSongs[i];
        }
    }

    this.chooseSongs = function(number) {
        this.newSongs = [];
        if (number > songs.length) {
            number = songs.length;
        }

        if (number > 0) {
            while (this.newSongs.length < number) {
                this.rand = this.getRandom(songs.length - 1);
                this.songName = songs[this.rand].name;

                if (this.newSongs.indexOf(this.songName) == -1 && !(this.isInHist(this.songName)) && this.localHistory.indexOf(this.songName) == -1 ) { 
                    this.newSongs.push(this.songName);
                }
            }
            this.addLocalHistory(this.newSongs);
        }
    };

    this.addHistory = function(newSongs) {
        this.histSongs = [];
        for (i=0;i<newSongs.length;i++) {
            this.histSongs[i] = newSongs[i];
        }
        history.push(this.histSongs);
        if (history.length >2) {
            history.shift();
        }
        this.localHistory = [];
    };
};

当我点击它时,如何让<table>中的一个或多个歌曲名称保持不变?当我再次点击它时,让它与其他人一起改变。

点击此按钮后歌曲会改变:

<button class="btn btn-default" ng-click="ctrl.chooseSongs(ctrl.number)">
    Refresh <i class="fa fa-refresh"></i>
</button>

我尝试创建一个获取点击歌曲的$ index并创建一系列点击歌曲的函数。每当我点击«刷新»时,我都试图替换«newSongs»数组中的索引的歌曲。

0 个答案:

没有答案