Я работаю с руководством по стилю John Papa. https://github.com/johnpapa/angular-styleguide#style-y061 . Мне нравится стиль, и кажется, что мой код намного легче читать. Однако я следил за отдельными вызовами данных.
В этом Джон Папа помещает возвращаемый объект в начало службы. Внутри операторов функций создаются внизу и поднимаются наверх при вызове функции.
Функция getCustomerListComplete утешает файл Json, как и следовало ожидать. Я обязательно вернул response.data.
Проблема в том, что оператор return не отправляет данные контроллеру.
return {
getCustomers: getCustomers
}
Начало службы ******
(function() {
angular
.module('app.services',[])
.factory('customersFactory', customersFactory);
function customersFactory($http, $log) {
return {
getCustomers: getCustomers
};
function getCustomers(){
$http.get('./Services/customers.json')
.then(getCustomerListComplete)
.catch(getCustomerListFailed);
function getCustomerListComplete(response) {
console.log('response.data',response.data);
return response.data;
}
function getCustomerListFailed(error) {
console.log('error', error);
}
}
}
}());
Внутри моего контроллера я получаю ошибки в обещании (тогда). Отметив, что он не может читать undefined.
Cannot read property 'then' of undefined
Вот мой контроллер. Я считаю, что я устанавливаю его и использую его правильно. CustomerFactory внедряется. Вызывается активная функция. Не уверен, в чем может быть проблема.
(function() {
'use strict';
angular
.module('app.customers')
.controller('CustomerController', CustomerController);
function CustomerController($stateParams, customersFactory) {
var vm = this;
vm.customers = [];
vm.orders = null;
// table sorting
vm.sortBy = 'name';
vm.reverse = false;
vm.doSort = doSort;
activate()
function activate() {
return getCustomersList().then(function() {
console.log('activated')
});
}
function getCustomersList() {
return customersFactory.getCustomers()
.then(function(data) {
vm.customers = data;
return vm.customers;
});
}
function doSort(propName) {
vm.sortBy=propName;
vm.reverse=!vm.reverse
}
}
})();