我有一个控制器,它位于一个名为EmployeeCtrl.js
的单独文件中。在此控制器中,我有一个名为convertJsonDate
的过滤器,可将JsonResult date format
转换为普通格式MM/dd/yyyy hh:mm:ss
。
我现在的问题是,如何在将来在不同的控制器中重复使用此过滤器?我已经读过你可以在一个单独的js文件filters.js
中添加你的过滤器并将其注入你的控制器,但我不知道实现它。
TIA
app.js
(function () {
'use strict';
angular.module('app', []);
})();
EmployeeCtrl.js
(function () {
'use strict';
var app = angular.module('app');
app.filter('convertJsonDate', ['$filter', function ($filter) {
return function (input, format) {
return (input) ? $filter('date')(parseInt(input.substr(6)), format) : '';
};
}]);
app.controller('app.EmployeeController', ['$scope', 'app.EmployeeService', function ($scope, EmployeeService) {
GetAllEmployee();
$scope.sortColumnBy = function (keyname) {
$scope.sortKey = keyname;
$scope.reverse = !$scope.reverse;
}
$scope.employee = {
employeeId: '',
firstName: '',
lastName: '',
password: '',
daysPerWeek: 0,
active: true,
departmentId: 0,
accountTypeId: 0
};
$scope.clear = function () {
$scope.employee.employeeId = '';
$scope.employee.firstName = '';
$scope.employee.lastName = '';
$scope.employee.password = '';
$scope.employee.daysPerWeek = 0;
$scope.employee.active = false;
$scope.employee.departmentId = 0;
$scope.employee.accountTypeId = 0;
};
function GetAllEmployee() {
var getEmployeeData = EmployeeService.getEmployees();
getEmployeeData.then(function (employee) {
$scope.employees = employee.data;
}, function () {
alert('Error in getting employee records');
});
};
}]);
})();
使用convertJsonDate过滤器
<div ng-app="app">
<div ng-controller="app.EmployeeController">
.....
<tbody>
<tr ng-repeat="e in employees | orderBy:sortKey:reverse | filter:searchKeyWord">
<td>{{e.AccountDateExpired | convertJsonDate:'MM/dd/yyyy hh:mm:ss'}}</td>
</tr>
</tbody>
....
</div>
</div>
引擎收录
Index.chtml http://pastebin.com/aXDSmYAV
EmployeeCtrl.js http://pastebin.com/eQhRREPy
app.js http://pastebin.com/1GB4uhvx
答案 0 :(得分:1)
如果它们位于同一文件或不同文件中,则无关紧要。只要两者都在同一模块中,或者过滤器位于依赖项中包含的模块中,您就可以使用它。
我建议在这里有3个文件:一个声明你的模块,一个用于你的控制器,一个用于你的过滤器:
<强> module.js 强>
var app = angular.module('app', []);
<强> filters.js 强>
var app = angular.module('app');
app.filter('convertJsonDate', ['$filter', function ($filter) {
return function (input, format) {
return (input) ? $filter('date')(parseInt(input.substr(6)), format) : '';
};
}]);
<强> controllers.js 强>
var app = angular.module('app');
app.controller('app.EmployeeController', ['$scope', 'app.EmployeeService', function ($scope, EmployeeService) {
GetAllEmployee();
$scope.sortColumnBy = function (keyname) {
$scope.sortKey = keyname;
$scope.reverse = !$scope.reverse;
}
$scope.employee = {
employeeId: '',
firstName: '',
lastName: '',
password: '',
daysPerWeek: 0,
active: true,
departmentId: 0,
accountTypeId: 0
};
$scope.clear = function () {
$scope.employee.employeeId = '';
$scope.employee.firstName = '';
$scope.employee.lastName = '';
$scope.employee.password = '';
$scope.employee.daysPerWeek = 0;
$scope.employee.active = false;
$scope.employee.departmentId = 0;
$scope.employee.accountTypeId = 0;
};
function GetAllEmployee() {
var getEmployeeData = EmployeeService.getEmployees();
getEmployeeData.then(function (employee) {
$scope.employees = employee.data;
}, function () {
alert('Error in getting employee records');
});
};
}]);
})();
答案 1 :(得分:0)
如果您使用的是同一个应用,则可以重复使用它们 ...这里,
var app = angular.module('app');
您可以像在此应用程序下的任何其他控制器的当前控制器中使用此过滤器一样使用此过滤器