在阅读约mocking server dependancies in JS and AngularJS apps后,我正在重构我的小型AngularJS应用程序中的主控制器。在AngularJS中使用ng-repeat调用和迭代函数返回值
在我的标记我遍历使用ng-repeat
指令中的“主要事件”:
这是我的AngularJS模块,重构库和控制器。
但是我无法以这种方式访问主事件数组 - 我错过了什么?
// Module
var app = angular.module('MainEventApp', ['cloudinary']);
// Repository
app.service('MainEventRepository', ['$http', function($http) {
this.$http = $http;
this.getMainEvents = function() {
return [
{
id: 1,
roman_numeral: 'I',
name: 'Hulk Hogan & Mr T vs Rowdy Roddy Piper & Paul Orndorff',
venue: 'Madison Square Garden',
state: 'New York',
attendance: 19121
},
{
id: 2,
roman_numeral: 'II',
name: 'Hulk Hogan vs King Kong Bundy',
venue: 'Split-Transmission',
state: 'New York/Illinois/California',
attendance: 40085
}
];
};
}]);
// Controller
app.controller("MainEventCtrl", ['$scope', 'MainEventRepository', function ($scope, MainEventRepository) {
MainEventRepository.getMainEvents().then(function(main_events) {
//$scope.main_events = main_events;
return main_events;
});
}]);
我的实时应用程序可以看到here(尚未重构)。
请附上您的标记。如果将方法应用于'this',则需要在路由器或ng控制器中使用controllerAs语法。 – Martin 2014-10-16 17:18:25