Логика отдельного контроллера и заводская логика в угловых

Я не хочу, чтобы весь мой код был в одном огромном контроллере, поэтому я пытаюсь немного его реструктурировать, но я сталкиваюсь с этой ошибкой

Argument 'protocolController' is not a function, got undefined

У меня есть свой завод

/*
    FACTORY THAT HANDLES ALL REQUESTS.
*/

function asyncFactory() {

    var factory = this;

    factory.list = [];

    this.update = function(restUrl) {
        return $http.get(restUrl)
        .success(function(data){
            factory.list = data;
        })
        .error(function(data){
            console.log('Error: ' + data);
        });
    };

    this.add = function(formData, restUrl){
        $http.post(restUrl, JSON.stringify(formData))
        .success(function(data){
            console.log('Success: ' + data);
        })
        .error(function(data){
            console.log('Error: ' + data);
        });
    };

    this.remove = function(value, restUrl){
        // get index of clicked object
        var index = this.protocolList.indexOf(value);

        // Remove object with given index from array
        factory.list.splice(index, 1);

        var url = restUrl + value.id;

        // delete it from DB
        $http['delete'](url);

        console.log('Deleted object with ID ' + value.id);
    };

    // Change existing object
    this.change = function(value, restUrl) {

        // get index of clicked object
        var index = this.protocolList.indexOf(this.activeObject);

        var url = restURL + value.id + '/' + value.name;
        console.log('url: ' + url)
        // change it before store it to DB
        $http['put'](url);

        console.log('Changed object with ID ' + this.activeObject.id);

    };
}

и мой контроллер, который использует фабрику

/*
    PROTOCOL CONTROLLER
*/
function protocolController(asyncFactory){

    var controller = this;
    controller.list = asyncFactory.list;
    controller.formData = {};

    this.getUpdatedList = function() {
        asyncFactory.update('../protocols')
        .then(function(data){
            controller.list = data;
        });
    };

    this.addData = function(formData){
        asyncFactory.add(formData, '../protocols');
    };

    this.removeData = function(value){
        asyncFactory.remove(value, '../protocols');
    };

    this.editData = function(value){
        asyncFactory.change(value, '../protocols')
    }
}

И назовите это так:

/*
    MAIN - WHERE THE CONTROLLER AND FACTORY IS CALLED
*/
var app = angular.module('TransactionMonitoring', [])
.controller('protocolController', protocolController(asyncFactory));

1 ответ

Решение

Изменить:

/ * ГЛАВНАЯ - ГДЕ ВЫЗЫВАЕТСЯ КОНТРОЛЛЕР И ФАБРИКА */

var app = angular.module('TransactionMonitoring', [])
.controller('protocolController', protocolController(asyncFactory));

Чтобы,

/ * ГЛАВНАЯ - ГДЕ ВЫЗЫВАЕТСЯ КОНТРОЛЛЕР И ФАБРИКА */

var app = angular.module('TransactionMonitoring', [])
.controller('protocolController', function(asyncFactory){

  var controller = this;
    controller.list = asyncFactory.list;
    controller.formData = {};

    this.getUpdatedList = function() {
        asyncFactory.update('../protocols')
        .then(function(data){
            controller.list = data;
        });
    };

    this.addData = function(formData){
        asyncFactory.add(formData, '../protocols');
    };

    this.removeData = function(value){
        asyncFactory.remove(value, '../protocols');
    };

    this.editData = function(value){
        asyncFactory.change(value, '../protocols')
    }
}


});

ОКАЗАНИЕ УСЛУГ:

app.service('asyncFactory',function ($http) {
 this.update = function(restUrl) {
        return $http.get(restUrl)
        .success(function(data){
            factory.list = data;
        })
        .error(function(data){
            console.log('Error: ' + data);
        });
    };

    this.add = function(formData, restUrl){
        $http.post(restUrl, JSON.stringify(formData))
        .success(function(data){
            console.log('Success: ' + data);
        })
        .error(function(data){
            console.log('Error: ' + data);
        });
    };

    this.remove = function(value, restUrl){
        // get index of clicked object
        var index = this.protocolList.indexOf(value);

        // Remove object with given index from array
        factory.list.splice(index, 1);

        var url = restUrl + value.id;

        // delete it from DB
        $http['delete'](url);

        console.log('Deleted object with ID ' + value.id);
    };

    // Change existing object
    this.change = function(value, restUrl) {

        // get index of clicked object
        var index = this.protocolList.indexOf(this.activeObject);

        var url = restURL + value.id + '/' + value.name;
        console.log('url: ' + url)
        // change it before store it to DB
        $http['put'](url);

        console.log('Changed object with ID ' + this.activeObject.id);

    };
}
}
)
Другие вопросы по тегам