我为所有名称字段的一些限制选项编写了一个工厂函数,我想在整个应用程序中使用/调用该工厂。我如何在全球范围内使用工厂?
这是我的代码:
checks.factory('pasteOptions',function($event) {
var clipboarddata = window.event.clipboardData.getData('text');
var regex = new RegExp("^[a-zA-Z.]+$");
var pastedData = window.event.clipboardData.getData('text/plain')
if(pastedData.match(regex) === null) {
event.preventDefault();
return false;
}
});
答案 0 :(得分:1)
如果您实际上尝试扩展HTML标记的功能,则可能需要使用指令。
要在整个应用程序中使用工厂,可以通过为工厂创建模块来完成:
"use strict";
(function () {
angular.module('example.module.with.factory', [
])
.factory('pasteOptions',function($event) {
var clipboarddata = window.event.clipboardData.getData('text');
var regex = new RegExp("^[a-zA-Z.]+$");
var pastedData = window.event.clipboardData.getData('text/plain')
if(pastedData.match(regex) === null) {
event.preventDefault();
return false;
}
});
})();
并将此工厂包含在整个应用程序的控制器中,将其声明为您希望使用它的控制器中的参数:
"use strict";
(function () {
angular.module("other.module", [
"example.module.with.factory", // Name of the module with the factory
"ui.router"
])
// Include the name of your factory as a parameter to the controller
.controller("otherModule.ctrl", ["$scope", "pasteOptions",
function($scope, pasteOptions) {
// do stuff with your factory
}]);
})();