AngularJS ViewModel在服务中未定义

时间:2014-11-18 12:13:31

标签: angularjs viewmodel angularjs-factory

我已经花了相当长的时间,但似乎我没有去哪里......我正在尝试创建一个模型/视图模型,我正在注入服务,但模型对象总是未定义。

'use strict';

define(['app'], function (app) {
    (function() {
        var taskVm = function(data) {
            this.firstName = data == null ? 'undefined' : (data.firstName == undefined ? '' : data.firstName),
                this.lastName = data == null ? 'undefined' : (data.lastName == undefined ? '' : data.lastName),
                this.contactNumber = data == null ? 'undefined' : (data.contactNumber == undefined ? '' : data.contactNumber),
                this.days = data == null ? 'undefined' : (data.taskStartedOn == undefined ? 1 : new Date().getDate() - new Date(data.taskStartedOn)),
                this.taskName = data == null ? 'undefined' : (data.taskName == undefined ? '' : data.taskName),
                this.taskId = data == null ? 'undefined' : (data.taskId == undefined ? 0 : data.taskId),
                this.processInstanceId = data == null ? 'undefined' : (data.processInstanceId == undefined ? 0 : data.processInstanceId),
                this.passportNumber = data == null ? 'undefined' : (data.passportNumber == undefined ? '' : data.passportNumber)
        };

        taskVm.prototype.setAgentData = function(data) {
            return new taskVm(data)
        };

    taskVm.prototype.object = function() {
        return {
            firstName: this.firstName,
            lastName: this.lastName,
            contactNumber: this.contactNumber,
            days: this.days,
            taskName: this.taskName,
            taskId: this.taskId,
            processInstanceId: this.processInstanceId,
            passportNumber: this.passportNumber
        };

    }
    app.factory('taskVm', [taskVm]);
}());

});

这是注入服务......

'use strict';

define(['app'], function (app) {

    var taskService = function($http, $q, $timeout, config, taskVm) {
        var taskFactory = {}, tasks = [];

        taskFactory.insertTask = function() {};

        taskFactory.getTask = function() {
         return  $http.get(config.serviceBase + config.task.get.taskList, { timeout: 5000, withCredentials: true }).then(
                    function (result) {

                        angular.forEach(result.data.list, function (item) {
                            var currentItem = new taskVm(item);
                            console.log(item);
                            console.log(currentItem);
                        });
                        return result.data;
                    });
                    }

           return taskFactory;
    };

    app.factory('taskService', ['$http', '$q', '$timeout', 'config', 'taskVm', taskService]);
});

但taskVm对象始终未定义。任何指针都会非常受欢迎。

0 个答案:

没有答案