为什么角度工厂没有按预期工作

时间:2013-07-11 13:54:31

标签: javascript angularjs

下面是几乎有效的简单角度工厂。我想重置我的模型,我尝试添加一个名为“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();

} ] );

1 个答案:

答案 0 :(得分: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();

} ] );
相关问题