javascript - ngResource remove record from query object with $delete -


fairly new angular. want use angular's $resource library consume our api services. i'm little lost on proper way delete record obtained via query() method. specifically, have endpoint user notifications. want to, on page load, user notifications, use ng-repeat loop on results , display notifications in nav bar. when user clicks remove icon, corresponding notification should deleted. here's stripped down version of code have:

js:

angular.module('myapp', ['ngresource']).factory('notifications',function($resource){     return $resource('/apiv2/user/notifications/:id', {id:'@id'}); }).controller('navigationcontroller',['$scope','notifications',function($scope, notifications){     $scope.notifications = notifications.query();      $scope.deletenotification = function(notification){         notification.$delete();     }; }]); 

html:

<ul>     <li ng-repeat="notification in notifications">         <i class="icon-remove" ng-click="deletenotification(notification)"></i>     </li> </ul> 

with code, when user clicks on remove icon, individual notification object passed deletenotification method , deleted backend via api. until point, works intended. however, if @ $scope.notifications object after fact, notification deleted remains broken data:

{$promise:undefined, $resolved:true}

ideally, want record wiped object returned via .query() method reflect state on end, without having new .query().

any appreciated! apologize vague descriptions and/or incomplete/innaccurate code, typed memory via phones keyboard whilst out @ dinner, god knows if missed something.

better way of doing it: (see angularjs ngresource delete event)

$scope.deletenotification = function (index) {   $scope.notifications[index].$delete();   $scope.notifications.splice(index, 1); } 

and in markup do

ng-click="deletenotification($index)" 

there better way this throws console error (but still works), doing:

$scope.notifications = notifications.query();  $scope.deletenotification = function(notification){     notification.$delete();     $scope.notifications = $scope.notifications.filter( function(n)         return (n != notification);     }); // filter }; 

if use underscore there more beautiful way write remove thing.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -