我有这个角度循环和控制器:
<li class="list-group-item" ng-repeat="post in posts | filter: post._creator = getCurrentUser"></li>
和控制器:
$http.get('/api/posts').success(function(posts) {
$scope.posts = posts;
$scope.getCurrentUser = Auth.getCurrentUser()._id;
});
可以在控制器中注入滤波器吗?
最佳做法是什么?
答案 0 :(得分:0)
我创建了一个专用文件filers.js并将过滤器注册码放入文件中。
'use strict';
angular.module('ftFilters', [])
.filter('rub', function() {
return function (value) {
if (value === undefined || value === null){
return 'не задана';
}
return value.toFixed(2).toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1 ").replace('.','<small>,') + '</small> руб';
};
})
.filter('rudate', function($filter) {
var angularDateFilter = $filter('date');
return function(theDate) {
return angularDateFilter(theDate, 'dd.MM.yy HH:mm:ss');
}
});
然后您必须将此模块添加到应用程序的依赖项中,例如
'use strict';
// Init the application configuration module for AngularJS application
var ApplicationConfiguration = (function() {
// Init module configuration options
var applicationModuleName = 'ft';
var applicationModuleVendorDependencies = ['ngResource', 'ngCookies', 'ngAnimate', 'ngTouch', 'ngSanitize',
'ui.router', 'ui.bootstrap', 'ui.utils', 'vr.directives.slider', 'ya.treeview', 'ui.select', 'ftFilters' ];
// Add a new vertical module
var registerModule = function(moduleName) {
// Create angular module
angular.module(moduleName, []);
// Add the module to the AngularJS configuration file
angular.module(applicationModuleName).requires.push(moduleName);
};
return {
applicationModuleName: applicationModuleName,
applicationModuleVendorDependencies: applicationModuleVendorDependencies,
registerModule: registerModule
};
})();
之后你可以随处使用