3

I have a simple example where I am exptecting my paragraphs to filter out values based on unique age, but I get the unknown provider error. How ?

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
    <p ng-repeat="x in persons | unique: 'age'">{{x.name}}</p>
<script>
//App declaration
var app = angular.module('myApp',[]);
//Controller Declaration
app.controller('myCtrl',function($scope){
    $scope.persons = [{name: "Peter",age:23},{name:"Laila",age:25},{name:"Rosy",age:23}];
});
</script>
</body> 
</html> 

enter image description here

3

2 Answers 2

2

You need to inject 'ui.directives' and 'ui.filters' module to your app

var app = angular.module('myApp',['ui.directive', 'ui.filters']);

Since ui.directives and ui.filters modules are present in AngularUI, you will also need to refer angular-ui.js in your application

<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.min.js"></script>

Your full code should look something like this

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
    <p ng-repeat="x in persons | unique: 'age'">{{x.name}}</p>
<script>
//App declaration
var app = angular.module('myApp',['ui.directives','ui.filters']);
//Controller Declaration
app.controller('myCtrl',function($scope){
    $scope.persons = [{name: "Peter",age:23},{name:"Laila",age:25},{name:"Rosy",age:23}];
});
</script>
</body> 
</html> 
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think you need ui.directives module here, and ui version is very outdated v0.4.0 - 2013-02-15. It doesn't mean that unique won't work, but rather than adding overkill dependency, it's better to use just the filter, or write your own.
@sbedulin I agree with you. I gave the answer cause the question was why the unknown provider error was being thrown.
2

That because the unique filter can currently be found as part of AngularJs UI Utils. To make this work you must include it as an additional reference in your module angular.module('myApp', ['ui', 'ui.filters']);

Hope this help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.