下面是我的角度应用程序。TypeError:无法读取未定义的属性'then'#2
(function() {
"use strict";
angular
.module("app.core", ['ngRoute'])
.controller("dataCtrl", dataCtrl);
dataCtrl.$inject = ['$scope','$location','dataService'];
/* @ngInject */
function dataCtrl($scope, $location, dataService) {
var methods = {
'Get': GetCustomers,
}
return methods.Get();
function GetCustomers(id) {
alert('test');
var scarData = dataService.read(id, "").then(function (data, err) {
console.log(data);
});
}
}
}());
服务
(function() {
'use strict';
angular
.module('app.service')
.factory('dataService', dataService);
dataService.$inject = ['$http', '$q'];
function dataService($http, $q) {
var service = {
create: create,
read: read,
update: update,
remove: remove
}
return service;
function read(id,type) {
return
$http.get("APIURL")
.then(success)
.catch(exception);
function success(response) {
}
function exception(ex) {
}
}
function create() {}
function update() {}
function detete() {}
}
})`
虽然调用来自控制器的服务我得到下面的错误。
TypeError: Cannot read property 'then' of undefined
此外,请在页面加载时建议更好的方式来调用控制器的get函数。
移动'success'和返回语句之前'exception'功能之间添加一个换行符。 – Hoyen