javascript - AngularJS push() error -
i have problem little angularjs app i'm developing. i'm jquery dev. go easy on me ;)
i have factory getting categories through $http call:
app.factory('simplefactory', function($http, $routeparams) { var categories = []; var factory = {}; factory.getcategories = function() { return $http({ url: '/system/getlist/', data: { id : pageid }, method: "post", }) .success(function(adddata) { categories = adddata; }); } return factory; }); my controller creates scope getting data factory:
app.controller('loadcategories', function ($scope, simplefactory) { $scope.categories = []; init(); function init() { $scope.categories = simplefactory.getcategories(); } });
now have 2nd scope triggered view (createcategory()) insert new item in cateogories. want push new item existing $scope.categories. trying way:
$scope.createcategory = function(cat,catname,catlvl,cattype) { var catname = catname; var parentid = cat.id; $http({ url: '/system/createcategory/', data: { catname : catname, parentid : parentid, pageid: pageid, catlvl: catlvl, cattype: cattype }, method: "post", }) .success(function(adddata) { $scope.categories.push(adddata); }); } this last controller lives in loadcategories controller.
the problem:
when try push() inside $scope.categories following error:
typeerror: object # has no method 'push'
does have idea why error? doing stuff not correct?
the ajax call completes , success callback triggered, goes wrong push().
i'm learning angularjs, please patient :)
$scope.categories = simplefactory.getcategories() return promise object, not array. , promise object have no method push(). change getcategories() method return array.
Comments
Post a Comment