angularjs - Update Angular View dynamically when MongoDB collection length changes -
i wondering how angular watches database collection changes in mongo db. have variable tracking number of items in mongodb collection , front end view interpolate number whenever new item added collection front end. possible?
example:
var item = $resource('/api/items'), allitems = item.query(function(results) { return results; }); $scope.alltodos = allitems.length;
front end:
<p ng-show="alltodos > 0">total items: {{alltodos}}</p>
let's assume had way of adding items via form on front end, submitting them database. how {{alltodos}} number dynamically increase (++) when add new item? in backbone.js, know there collection watch events, ie:
var collection = backbone.collection.extend({ //code... }), c1 = new collection(); c1.bind("add", function() { //do when add collection });
is possible angular? i'm sure is, please excuse green-ness. thanks!
$resource.query
returns empty object immediately. allitems
not expect be. once operation complete allitems
populated data. there no need return callback
one way set alltodos
inside callback:
item.query(function(results) { $scope.alltodos = results.length; });
or eliminate variable alltodos
, make items visible in template (should part of controller's $scope
).
var item = $resource('/api/items'); $scope.allitems = item.query();
and in html
<p ng-show="allitems.length > 0">total items: {{allitems.length}}</p>
Comments
Post a Comment