我想在ng-options指令中使用唯一过滤器,但是当我尝试添加脚本文件时出现此错误,我按以下方式添加脚本文件。
我从这里下载了unique.js文件https://github.com/angular-ui/angular-ui-OLDREPO/blob/master/modules/filters/unique/unique.js 然后我像这样添加脚本src
<script src="/SpringMVCTemplateAnn/resources/bower_components/bootstrap/dist/js/unique.js"></script>
然后我在我的应用程序中添加模块
angular.module('MyApp', ['ngRoute', 'ui.bootstrap', 'smart-table', 'http-auth-interceptor', 'ui.filters']);
但我收到以下错误
Error: [$injector:nomod] http://errors.angularjs.org/1.4.8/$injector/nomod?p0=ui.filters
Error: [$injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=MyApp&p1=%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.8%2F%24injector%2Fmodulerr%3Fp0%3Dui.filters%26p1
这是unique.js文件的内容
/**
* Filters out all duplicate items from an array by checking the specified key
* @param [key] {string} the name of the attribute of each object to compare for uniqueness
if the key is empty, the entire object will be compared
if the key === false then no filtering will be performed
* @return {array}
*/
angular.module('ui.filters').filter('unique', function () {
return function (items, filterOn) {
if (filterOn === false) {
return items;
}
if ((filterOn || angular.isUndefined(filterOn)) && angular.isArray(items)) {
var hashCheck = {}, newItems = [];
var extractValueToCompare = function (item) {
if (angular.isObject(item) && angular.isString(filterOn)) {
return item[filterOn];
} else {
return item;
}
};
angular.forEach(items, function (item) {
var valueToCheck, isDuplicate = false;
for (var i = 0; i < newItems.length; i++) {
if (angular.equals(extractValueToCompare(newItems[i]), extractValueToCompare(item))) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
newItems.push(item);
}
});
items = newItems;
}
return items;
};
});