Angular Service中的Singleton JS对象

时间:2016-05-16 07:25:30

标签: angularjs

我正在尝试将方法添加到Object的原型中,该原型将在单件服务中使用,并且在创建服务时仅启动一次。

angular
        .module('app.steps')
        .factory('stepsService', stepsService);

    stepsService.$inject = [];

    /* @ngInject */
    function stepsService() {
        var steps = new Steps(1,3);

    function Steps(current_step, total_steps) {
        this.c_step = current_step;
        this.t_step = total_steps;            
    }

    Steps.prototype = {
        addSteps: function (num) {
            this.c_step += num;
        },
        setLastStep: function () {
            this.lastStep = this.c_step = this.t_step;
        }
    };

    var service = {
        steps: steps           
    };       

    return service; 
    }

我的问题是虽然对象是成功创建和启动的,但方法并不存在。 缺少什么?

1 个答案:

答案 0 :(得分:0)

As mentioned in the commentsvar steps = new Steps(1,3);应该在Steps.prototype = {....}

之后