javascript - Remove Item from Firebase -
i'm trying understand how remove item firebase. i've set function (createprovider
) create item , can't figure out how go removing item.
html
<form role="form" name="createproviderform"> <input type="text" ng-model="title"> <button type="submit" ng-click="createprovider()">submit</button> </form> <div ng-repeat="provider in providers"> <h3>{{ provider.title }}</h3> <button type="button" ng-click="removeprovider()">remove</button> </div> </div>
js
var rootref = new firebase(fburl); var providersref = rootref.child('providers'); $scope.newprovider = {}; $scope.providers = []; providersref.on('child_added', function(snapshot) { $timeout(function() { var snapshotval = snapshot.val(); console.log(snapshotval); $scope.providers.push({ title: snapshotval.title }); }); }); $scope.createprovider = function() { var newprovider = { title: $scope.title }; providersref.push(newprovider); }; $scope.removeprovider = function() { };
i've got far creating function called removeprovider
can't work out put inside it. realise i've got somehow target particular item , remove list. i'm not sure how.
any appreciated. in advance!
to remove item firebase, need know name()
, automatically generated when call push()
add new item firebase.
so need bind name scope:
$scope.providers.push({ name: snapshot.name(), title: snapshotval.title });
you pass name call removeprovider
html:
<div ng-repeat="provider in providers"> <h3>{{ provider.title }}</h3> <button type="button" ng-click="removeprovider(provider.name)">remove</button> </div>
and use call remove
on firebase:
$scope.removeprovider = function(name) { providersref.child(name).remove(); };
as @sharpieone commented, , many other things automatically handled if use angularfire library (and in case $asarray()
method).
Comments
Post a Comment