0

下面是几乎可以工作的简单角度工厂。我想重置我的模型,我尝试添加一个名为“ resetModel”的方法,但它不起作用,因为它没有重置模型属性。有人可以解释为什么吗?

app.factory('programLocationModel', [ "$rootScope", function ($rootScope)
{

    var ProgramLocationModel = function()
    {
        this.name            =  "All Programmes";
        this.description     =  "";
        this.category        =  "";
        this.series          =  {};
        this.channel         =  {};
        this.duration        =  "";
        this.airTime         =  "";
        this.seriesName      =  "";
        this.url             =  "../assets/images/nhkw_thumbnail.jpg"; //Default client logo

    }

    ProgramLocationModel.prototype.update           = function( data )
    {

        this.name            =  data.name;
        this.description     =  data.description;
        this.category        =  data.category;
        this.series          =  data.series;
        this.seriesName      =  data.seriesName;
        this.channel         =  data.channel;
        this.duration        =  data.duration;
        this.airTime         =  data.airTime;
        this.url             =  $rootScope.resturl + '/graph/' + data.id + '/thumbnail?access_token=' + $rootScope.token;
    }


    ProgramLocationModel.prototype.resetModel = function () {

        ProgramLocationModel();
    }

    return new ProgramLocationModel();

} ] );
4

1 回答 1

1

您的 resetModel 函数仅调用构造函数,而不对调用该方法的实际实例做任何事情。您的 resetModel 函数应该修改this的属性,就像您已经在构造函数和更新方法中所做的那样。这是一个简单的方法:

app.factory('programLocationModel', [ "$rootScope", function ($rootScope)
{

    var ProgramLocationModel = function()
    {
       this.resetModel();
    }

    ProgramLocationModel.prototype.update           = function( data )
    {

        this.name            =  data.name;
        this.description     =  data.description;
        this.category        =  data.category;
        this.series          =  data.series;
        this.seriesName      =  _seriesName;
        this.channel         =  data.channel;
        this.duration        =  data.duration;
        this.airTime         =  data.airTime;
        this.url             =  $rootScope.resturl + '/graph/' + data.id + '/thumbnail?access_token=' + $rootScope.token;
    }


    ProgramLocationModel.prototype.resetModel = function () {
        this.name            =  "All Programmes";
        this.description     =  "";
        this.category        =  "";
        this.series          =  {};
        this.channel         =  {};
        this.duration        =  "";
        this.airTime         =  "";
        this.seriesName      =  "";
        this.url             =  "../assets/images/nhkw_thumbnail.jpg"; //Default client logo
    }

    return new ProgramLocationModel();

} ] );
于 2013-07-11T14:07:46.170 回答