我写了下面这段代码,但它给出了一个错误。 https://jsfiddle.net/5e077ymu/1/
错误是
angular.js:14525 TypeError: FirstAncientTheatreFactory.getFirstTheatreDetails is not a function
services.js
.factory('FirstAncientTheatreFactory',function(){
var firstheatrefact = {};
var first_ancient_theatre = [
{
"id":0,
"description":"The first excavation research, which identified the first ancient theater, took place in 1910 when the then Archbishop of Antiquities, Ap. Arvanitopoulos, revealed part of his stage. At the beginning of the 20th century, over a monument (which was not visible), a street was built and various buildings (houses, workshops, shops) were built, some of which, with their deep foundations, caused irreparable damage to the monument. Since 1977, a systematic effort has been made for the complete disclosure and promotion of the monument with excavations and expropriations of buildings, which continues, from the EO of Prehistoric and Classical Antiquities",
"image":"img/first_ancient_theatre/first_ancient_theatre1.jpg"
}
]
firstheatrefact.getFirstTheatreDetails = function(index){
return first_ancient_theatre[index];
}
return first_ancient_theatre;
})
.factory('feedbackFactory', ['$resource', 'baseURL', function ($resource, baseURL) {
var feedfac = {};
feedfac.getFeedback = function () {
return $resource(baseURL + "feedback/:id", null, {'update': {method: 'PUT'}});
};
return feedfac;
}])
controller.js
.controller('FirstAncientTheatreController', ['$scope', 'FirstAncientTheatreFactory', function ($scope, FirstAncientTheatreFactory) {
$scope.message="Loading ...";
$scope.firstTheatreZero = FirstAncientTheatreFactory.getFirstTheatreDetails(0);
}])
html页面
<div class="container" ng-contoller="FirstAncientTheatreController">
<div class="row">
<div class="col-xs-12">
<p style="font-size:18px;">{{firstTheatreZero.description}}</p>
</div>
</div>
</div>
我发现这有点奇怪,因为定义了函数getFirstTheatreDetails。我错过了什么?
答案 0 :(得分:1)
你做过return first_ancient_theatre;
,
虽然你本应该返回firstheatrefact
。
答案 1 :(得分:1)
您正在返回json变量而不是对象。更改
return first_ancient_theatre;
至return firstheatrefact;