javascript - removing an object in an array using Angular syntax -
this question has answer here:
i trying use angular manage list of tags through interface.
i have server-pulled list of tags in scope.
app.js
$scope.tags = [{id:1, name:'tag1', desc:'desc1'}, {id:2, name:'tag2', desc:'desc2'}, {id:3, name:'tag3', desc:'desc1'}, ...]; and display list using chunk of html code :
tags.html
<ul> <li ng-repeat="t in tags">{{ t.name }}</li> </ul> when click on <li> element want angular remove associated tag object. enhance code follow :
tags.html
<ul> <li ng-repeat="t in tags" ng-click="removetag($event);" >{{ t.name }}</li> </ul> app.js
$scope.removetag = function (event) { // following code used remove tag list // using underscore.js $scope.tags = _($scope.tags).filter(function (t) { return t.name !== event.srcelement.innerhtml }); } this working, wish there lighter way perform same task. , experience of angular still limited.
something great :
<ul> <li ng-repeat="t in tags" ng-click="tags - t">{{ t.name }}</li> <!-- more dream tho --> </ul>
try splicing array based on index in array ($index), this:
<ul> <li ng-repeat="t in tags" ng-click="tags.splice($index, 1)">{{t.name}}</li> </ul>
Comments
Post a Comment