javascript - AngularJS: Error Undefined -
what wrong code? expect see value of 1 beingness printed in console instead throws error stating self.list[0] undefined???
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"> </script> <script type="text/javascript"> angular.module('notesapp', []) .controller('mainctrl', ['itemservice', function(itemservice) { var self = this; self.list = function() { homecoming itemservice.list(); }; console.log(self.list[0].id); //<<-- why print undefined?? self.add = function() { itemservice.add({ id: self.list().length + 1, label: 'item ' + self.list().length }); }; }]) .factory('itemservice', [function() { var items = [ {id: 1, label: 'item 0'}, {id: 2, label: 'item 1'} ]; homecoming { list: function() { homecoming items; }, add: function(item) { items.push(item); } }; }]);
thank in advance answers. also, trying not blunt won't allow me set simple greeting @ start of question...
because @ point self.list
function!! when next executed,
self.list = function() { homecoming itemservice.list(); };
as expecting list there, maybe need execute function -- like
self.list = function() { homecoming itemservice.list(); }();
try simply!
console.log("see self.list=", self.list);
javascript angularjs
No comments:
Post a Comment