在角应用整合本地JavaScript类 [英] Integrating native JavaScript classes in an Angular app
本文介绍了在角应用整合本地JavaScript类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个本地JavaScript类:
I have a native JavaScript class:
var Holder = new function(elements) {
this.elements = elements;
this.anyFunction() {
// use of this.elements
};
};
如何使用它在角路?例如,如果我想使用方法:
How to use it in an Angular-way? For example, if I would like to use:
.controller('AnyController', ['Holder',
function (Holder) {
var elements = [
{id: 1, label: 'foo'},
{id: 2, label: 'bar'}
];
$scope.holder = new Holder(elements);
}])
我应该如何注册我的架
类呢?什么是选项(如果有的话)?
How should I register my Holder
class then? What are the options (if any)?
在并行,是的坏的在角应用使用本地JavaScript类(即,没有框架内整合它)?
In parallel, is it that bad to use native JavaScript classes in an Angular app (i.e. without integrating it within the framework)?
推荐答案
您可以返回一个类工厂
.factory('Holder', function() {
return (function (){
this.foo = foo;
this.bar = bar;
});
});
立即使用它
.controller('AnyController', ['Holder', function (Holder) {
var holder = new Holder();
}]);
修改
使用,而不是服务
工厂
,所建议的意见
EDIT
Use a factory
instead of a service
, as suggested in the comments
这篇关于在角应用整合本地JavaScript类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文