更新:

感谢您的回复!

我已经重写了代码:

(function () {
'use strict';
angular
.module('Services', []).factory('services', ['$http', function($http,services) {
    function services($http) {

        var serviceProvider = function () {
            this.data = [];
            this.errors = [];
        }
        var model = {
            getInstance:function(){ return new serviceProvider(); }
        }
            serviceProvider.prototype.init = function(){//put some init stuff here
            }
            serviceProvider.prototype.getFromRESTServer = function(msg,callback){
                return $http.jsonp("http://xxxxxxx/JSONEngine.php?callback=JSON_CALLBACK&action="+callback+"&"+msg);
            }
            return model;
        }
    }])
})();


我的控制器定义为:

var uniqueModelInstance = services.getInstance();
uniqueModelInstance.init();
uniqueModelInstance.getFromRESTServer("username="+$scope.username+"&password="+$scope.password,"register").success(function (data) {...}


他们正确吗?现在,我获得“无法读取未定义的属性'getInstance'”。

有什么建议吗?

提前致谢。
朱塞佩

我有这样定义的角度工厂:

services.factory('services', ['$http', function($http) {
    var service = {};
    return {
        getFromRESTServer: function (msg,callback){
        return $http.jsonp("http://myUrl/JSONEngine.php?callback=JSON_CALLBACK&action="+callback+"&"+msg);
        }
    }
}]);


和具有doLogin功能的控制器:

home.controller('registrazioneTraduttoreCtrl',  ['$scope', '$rootScope', '$window', 'services', '$location', 'customFactory',
function ($scope, $rootScope, $window, services, $location, customFactory) {

$scope.doLogin= function(username, password) {
    services.getFromRESTServer("username="+username+"&password="+password,"login").
    success(function (data) {
        if(data.jsonError != null || data.errCode != null)
        {
           alert (data.errMsg);
       }
       else {
           // DO STUFF...
    }).error(function(data, status) {
      console.error('Repos error', status, data);
  })

    .finally(function() {
      console.log("finally finished repos");
  });
}


}]);

getFromRESTServer也可以由另一个控制器中的另一个函数执行(我的html页面中有2个不同的Registration表单,然后它们调用doLogin函数)。

当我调试应用程序时,调试器从以下位置跳过:
   services.getFromRESTServer(“ username =” + username +“&password =” + password,“ login”)行(在doLogin函数中)到getFromRESTServer函数的末尾而无需进入,然后使用用户名和密码NULL和重新执行doLogin函数现在,它进入getFromRESTServer函数的核心。

有任何想法吗?

提前致谢。
朱塞佩

最佳答案

您可以通过返回任何工厂的新实例来执行此操作。查看此Plunker或尝试以下代码:

/**
 * Non singleton factory example
 *
 * @name        non-singleton-example
 * @author      Nils Gajsek <[email protected]>
 */
(function () {

    //use strict -> ECMAScript5 error reporting
    'use strict';


    // ################################################ angularJS Module define // ####################################

    /**
     * DB service, part of app module
     */
    angular
        .module('app.model', [])  // [-_-]
        .factory('model', ['$http', model]);


    /**
     * Model factory wrapper
     *
     * @param {object} $http
     *
     * @returns self
     */
    function model($http) {


        // ################################################## Vars // ##############################################

        var serviceProvider = function(){

            /**
             * Data store
             * @type {Array}
             */
            this.data = [];


            /**
             * Error store
             * @type {Array}
             */
            this.errors = [];
        }


        // ############################################### Functions // ############################################

        /**
         * Model instance provider handler
         * This object is returned on the end of this object
         */
        var model = {
            getInstance:function(){ return new serviceProvider(); }
        }


        /**
         * Model init function, provides
         */
        serviceProvider.prototype.init = function(){

            //put some init stuff here
        }


        /**
         * Example function
         *
         * @returns {{this}}
         */
        serviceProvider.prototype.someFunction = function(){
            //do some stuff with model
        }


        //return model -> non-singleton model instance object
        return model;
    }
})();


这就是您作为唯一实例接收它的方式。

 var uniqueModelInstance = model.getInstance();
 uniqueModelInstance.init();


或者更好(但是您需要通过调用init()函数来返回实例本身)

var uniqueModelInstance = model.getInstance().init();

10-04 16:37