下面是我的角度應用程序。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