JSFiddle - React, Tailwind, and code Playground
by BinaryMuse
HTML
<script src="http://underscorejs.org/underscore-min.js"></script>
<div ng-app="angularjs-starter" ng-controller="MainCtrl">
<div ng-repeat="tag in uniqueTags()">
<label class="checkbox">
<input type="checkbox" ng-model="filter.tag" />
{{tag}}
</label>
</div>
<div>
Add a new item to the list; specify tags (comma separated):
<input ng-model='newTags' ng-list>
<button ng-click="addItem(newTags)">Add</button>
</div>
</div>
JavaScript
var app = angular.module('angularjs-starter', []);
app.controller('MainCtrl', function($scope) {
$scope.list = [
{id: 0, tags: ['tag1', 'tag2']},
{id: 1, tags: ['tag2']},
{id: 2, tags: ['tag1', 'tag3', 'tag4']},
{id: 3, tags: ['tag3', 'tag4']}
];
$scope.uniqueTags = function() {
return _.chain($scope.list)
.pluck('tags') // [['tag1', 'tag2'], ['tag2'], ['tag1', 'tag3', 'tag4'], ['tag3', 'tag4']]
.flatten() // ['tag1', 'tag2', 'tag2', 'tag1', 'tag3', 'tag4', 'tag3', 'tag4']
.unique() // ['tag1', 'tag2', 'tag3', 'tag4']
.value();
};
$scope.addItem = function(tags) {
var newItem = {
id: _($scope.list).last().id + 1,
tags: tags
};
$scope.list.push(newItem);
$scope.newTags = [];
};
});