AngularJS - Get first element -


i have set service events:

app.service('eventservice', function (event) {          var self = {             'selectedevent': null,             'page': 1,             'hasmore': true,             'isloading': false,             'events': [],             'loadevents': function () {                 if (self.hasmore && !self.isloading) {                     self.isloading = true;                      var params = {                         'page': self.page,                         'place_id': "{{ request()->get('place_id') ?: '' }}",                         'category_id': "{{ request()->get('category_id') ?: '' }}",                         'date': "{{ request()->get('date') ?: '' }}",                         'date_from': "{{ request()->get('date_from') ?: '' }}",                         'date_to': "{{ request()->get('date_to') ?: '' }}",                     };                      event.get(params, function (data) {                         console.log(data);                         angular.foreach(data.data, function (event) {                             console.log('event: ' + event);                             self.events.push(new event(event));                         });                          if (!data.next_page_url) {                             self.hasmore = false;                         }                         self.isloading = false;                     });                 }             },             'loadmore': function () {                 if (self.hasmore && !self.isloading) {                     self.page += 1;                     self.loadevents();                 }             }         };          self.loadevents();          return self;     }); 

this controller:

app.controller('eventsctrl', function ($scope, $http, eventservice) {          $scope.events = eventservice;          $scope.loadmore = function () {              $scope.events.loadmore();         };          // $scope.loading = false;           $scope.selectevent = function (event) {             $scope.selectedevent = event;             //console.log('selezionato');         };      }); 

right selectedvalue empty , when user goes row variable fill. how can make sure if variabile not set first element should selected one?

i tried add new method in service:

'getfirst': function(){       self.selectedevent = self.events[0];  } 

but when in controller use:

$scope.selectedevent = eventservice.getfirst(); 

it empty.
looks when function called events not loaded yet.

how can fix?

edit:

this json backend:

{"total":40,"per_page":7,"current_page":1,"last_page":6,"next_page_url":"http:\/\/demo.meetmount.com\/it\/eventi\/getjsonevents?page=2","prev_page_url":null,"from":1,"to":7,"data":[{"id":1,"user_id":1,"place_id":5,"category_id":0,"title":"error sunt non sit.","description":"nisi modi ratione adipisci qui iure odio possimus. quisquam eos quidem atque animi aliquam rerum. inventore fugiat sed consequatur debitis expedita quo quis accusamus. ad officia vero temporibus voluptatum.","image":null,"date":"1983-03-12 00:00:00","difficulty_level":2,"price":null,"max_participants":null,"created_at":"2016-01-31 18:07:44","updated_at":"2016-01-31 18:07:44","deleted_at":null,"place":{"id":5,"name":"colere","slug":"colere","filename":null,"originalname":null,"latitude":"45.9742604","longitude":"10.0850681","region_id":9,"cap":"24020","created_at":"2016-01-31 18:07:43","updated_at":"2016-01-31 18:07:43","deleted_at":null}},{"id":2,"user_id":1,"place_id":7,"category_id":0,"title":"sunt quae dolore pariatur suscipit.","description":"aut ullam voluptatibus accusantium est. autem optio et culpa non. aut ratione dignissimos sequi enim et quae. reprehenderit veniam sunt natus mollitia in.","image":null,"date":"2012-12-12 00:00:00","difficulty_level":3,"price":null,"max_participants":null,"created_at":"2016-01-31 18:07:44","updated_at":"2016-01-31 18:07:44","deleted_at":null,"place":{"id":7,"name":"lizzola","slug":"lizzola","filename":null,"originalname":null,"latitude":"46.0240469","longitude":"10.0144382","region_id":9,"cap":"24020","created_at":"2016-01-31 18:07:43","updated_at":"2016-01-31 18:07:43","deleted_at":null}},{"id":3,"user_id":1,"place_id":17,"category_id":0,"title":"omnis fugit libero rem et.","description":"nihil eum dolorem perferendis autem. id id aperiam ex itaque nulla.","image":null,"date":"1976-12-01 00:00:00","difficulty_level":4,"price":null,"max_participants":null,"created_at":"2016-01-31 18:07:44","updated_at":"2016-01-31 18:07:44","deleted_at":null,"place":{"id":17,"name":"stelvio","slug":"stelvio","filename":null,"originalname":null,"latitude":"46.5333333","longitude":"10.4500000","region_id":9,"cap":"7536","created_at":"2016-01-31 18:07:43","updated_at":"2016-01-31 18:07:43","deleted_at":null}},{"id":4,"user_id":1,"place_id":6,"category_id":0,"title":"totam et distinctio eligendi corrupti necessitatibus error.","description":"aut modi et et ipsa facilis dolorem. provident qui aut quae tempora et distinctio. sunt ipsa libero blanditiis. occaecati doloribus tenetur laboriosam in.","image":null,"date":"1977-12-11 00:00:00","difficulty_level":1,"price":null,"max_participants":null,"created_at":"2016-01-31 18:07:44","updated_at":"2016-01-31 18:07:44","deleted_at":null,"place":{"id":6,"name":"livigno","slug":"livigno","filename":null,"originalname":null,"latitude":"46.5386360","longitude":"10.1357319","region_id":9,"cap":"23030","created_at":"2016-01-31 18:07:43","updated_at":"2016-01-31 18:07:43","deleted_at":null}},{"id":5,"user_id":1,"place_id":5,"category_id":0,"title":"expedita aspernatur provident amet suscipit dignissimos nostrum amet.","description":"sapiente qui quis omnis fugit qui consectetur aut. ab dolorem est error est fugiat. culpa dolore rem iusto delectus dolore.","image":null,"date":"1986-10-30 00:00:00","difficulty_level":2,"price":null,"max_participants":null,"created_at":"2016-01-31 18:07:44","updated_at":"2016-01-31 18:07:44","deleted_at":null,"place":{"id":5,"name":"colere","slug":"colere","filename":null,"originalname":null,"latitude":"45.9742604","longitude":"10.0850681","region_id":9,"cap":"24020","created_at":"2016-01-31 18:07:43","updated_at":"2016-01-31 18:07:43","deleted_at":null,"translations":[]}},{"id":6,"user_id":1,"place_id":2,"category_id":0,"title":"id officiis dolores quidem unde quo.","description":"quas enim ratione aut. nihil culpa placeat quisquam corrupti magnam repudiandae. qui deleniti quam molestiae autem itaque.","image":null,"date":"1987-11-10 00:00:00","difficulty_level":1,"price":null,"max_participants":null,"created_at":"2016-01-31 18:07:44","updated_at":"2016-01-31 18:07:44","deleted_at":null,"place":{"id":2,"name":"bormio","slug":"bormio","filename":null,"originalname":null,"latitude":"46.4663571","longitude":"10.3704671","region_id":9,"cap":"23032","created_at":"2016-01-31 18:07:43","updated_at":"2016-01-31 18:07:43","deleted_at":null}},{"id":7,"user_id":1,"place_id":11,"category_id":0,"title":"dolores voluptas id ad itaque voluptates molestias minus.","description":"modi et soluta porro voluptates @ nihil. quaerat commodi et temporibus esse harum autem reprehenderit. incidunt velit ut sit temporibus adipisci voluptatem.","image":null,"date":"1976-12-13 00:00:00","difficulty_level":3,"price":null,"max_participants":null,"created_at":"2016-01-31 18:07:44","updated_at":"2016-01-31 18:07:44","deleted_at":null,"place":{"id":11,"name":"piani di bobbio valtorta","slug":"piani-di-bobbio-valtorta","filename":null,"originalname":null,"latitude":"45.9728896","longitude":"9.5037049","region_id":9,"cap":"24010","created_at":"2016-01-31 18:07:43","updated_at":"2016-01-31 18:07:43","deleted_at":null}}]} 

this fiddle ( it's not working)

https://jsfiddle.net/15v107c3/

you're doing in right way, thing you're missing loadevents firstly.

try call first eventservice.loadevents(); , , can reference same variable fill events when call loadevents();

var eventserviceobj = eventservice.loadevents(); eventserviceobj.events[0];// first event here 

you need sure method loadevents called firstly populate events field.

you can make check if event not selected or empty, loadevents , select first one.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -