I am trying to use Angular to manage a list of Tags through an interface.
I have a server-pulled list of Tags in my scope.
app.js
$scope.Tags = [{id:1, name:'tag1', desc:'desc1'}, {id:2, name:'tag2', desc:'desc2'}, {id:3, name:'tag3', desc:'desc1'}, ...];
and I display the list using this chunk of html code :
tags.html
<ul>
<li ng-repeat="T in Tags">{{ T.name }}</li>
</ul>
When I click on the <li>
element I want angular to remove the associated Tag object.
Then I enhance my code as follow :
tags.html
<ul>
<li
ng-repeat="T in Tags"
ng-click="removeTag($event);"
>{{ T.name }}</li>
</ul>
app.js
$scope.removeTag = function (event) {
// the following code is just used to remove the Tag from the list
// using underscore.js
$scope.Tags = _($scope.Tags).filter(function (t) {
return t.name !== event.srcElement.innerHTML
});
}
This is working, but I wish there were a lighter way to perform the same task. And my experience of Angular is still limited.
Something like that would be great :
<ul>
<li ng-repeat="T in Tags" ng-click="Tags - T">{{ T.name }}</li>
<!-- this is more like a dream tho -->
</ul>