I have a div which shoes the names. I want to remove the duplicates in my array. I have done this with filter, but i want to know how we can extend this to build a directive.
<div ng-controller="MainController">
<ul>
<li ng-repeat="name in names | unique">
{{name}}
</li>
</ul>
</div>
Below is the 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){
}
}
})
How can i build a directive which removes duplicates from my array.
angular.equals(o1, o2);function. And to delete, usedelete names[names.indexOf(name)];.