I am currently working with a div that displays names and I need to remove any duplicates from my array. I have successfully achieved this with a filter, but now I would like to explore creating a directive to extend this functionality.
<div ng-controller="MainController">
<ul>
<li ng-repeat="name in names | unique">
{{name}}
</li>
</ul>
</div>
Below is the existing filter code.
angular.module('app')
.controller('MainController', ['$scope', function($scope){
$scope.names = ['a','b','c','d','a','c'];
}])
.filter('unique', function(){
return function(names){
var obj = {};
names.forEach(function(name){
obj[name] = null;
})
return Object.keys(obj);
}
})
.directive('unique', function(){
return {
link: function(scope, elem, attr){
}
}
})
I am seeking guidance on how to create a directive that will effectively remove duplicates from my array.