More problems with the "easy to learn" angularjs framework -
More problems with the "easy to learn" angularjs framework -
i trying simplest thing angularjs makes incredibly difficult.
this code here works:
.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); } };
the variable items declared @ top of mill , accessible in homecoming statement.
so why doesn't work:
.factory('itemservice', ['$http', function($http) { var self = this; self.items = []; $http.get('/api_job_inspections/1/edit').then(function(response) { //self.items = response.data; self.items = [ {id: 1, label: 'item 0'}, {id: 2, label: 'item 1'} ]; }, function(errresponse) { console.error('error while fetching notes'); }); homecoming { list: function() { homecoming self.items; }, add: function(item) { self.items.push(item); } };
i have tried using var items = [] in first illustration nil gets returned. using syntax nil gets returned. why? there seems scoping issue it?
thank in advance...
it doesn't seem scoping issue, rather async issue, did create sure ajax phone call complete?
try in controller doing following:
angular.module('myapp').controller('itemscontroller', ['itemservice', '$scope', function(items, $scope) { $scope.$watch(function() { homecoming items.list(); }, function(newval) { console.log(newval); }); }]);
p.s. instead of using mill can utilize service, it's wrapper around factory, allow utilize this.something = function
it's lot more convenient mill (imo).
angularjs
Comments
Post a Comment