Wednesday 15 May 2013

javascript - calling and iterating over function return value using ng-repeat in AngularJS -



javascript - calling and iterating over function return value using ng-repeat in AngularJS -

i refactoring main controller in little angularjs application after reading mocking server dependancies in js , angularjs apps.

in markup iterate on "main events" using ng-repeat directive:

this angularjs module, refactored repository , controller.

however unable access main event array in way - missing?

// module var app = angular.module('maineventapp', ['cloudinary']); // repository app.service('maineventrepository', ['$http', function($http) { this.$http = $http; this.getmainevents = function() { homecoming [ { 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; homecoming main_events; }); }]);

my live application can seen here (not yet refactored).

first, utilize main_events in markup need have property in controller scope.

second, service returns simple array, in controller utilize if homecoming promise.

so, controller code should linke this:

app.controller("maineventctrl", ['$scope', 'maineventrepository', function ($scope, maineventrepository) { $scope.main_events = maineventrepository.getmainevents(); }]);

and markup:

<li ng-repeat="event in main_events"></li>

javascript arrays angularjs http

No comments:

Post a Comment