简体   繁体   English

如何在Angular JS中声明不是服务但不会污染全局范围的对象?

[英]How to declare an object in Angular JS that isn't a service but doesn't pollute the global scope?

I've since discovered I can return instances of an object by having a get() method in my factory, that returns new service instances. 从那以后,我发现可以通过在工厂中使用get()方法返回对象的实例,该方法返回新的服务实例。

But what if my object isn't a service (talking semantics here). 但是,如果我的对象不是服务,该怎么办(在这里谈论语义)。 EG I have a page with many charts on it, the chart object (below) isn't (semantically) a service. 例如,我的页面上有很多图表,图表对象(如下)不是(实际上)不是服务。

So do I have to declare it using something like factory/provider/etc pattern? 那我是否必须使用factory / provider / etc模式来声明它? It feels wrong, as it's not actually a service. 感觉不对,因为它实际上不是服务。 But I need to reference it from my controller, so it needs to be injected, or accessible somehow. 但是我需要从控制器中引用它,因此需要将其注入或以某种方式访问​​。 & I don't want it to pollute the global scope. 我不希望它污染全球范围。

var Chart = function () {
    var self = this;
    this.initialize = function (name, clientMethod, usingDateRange, usesAnalytics, initCB, serviceCB, highchartsConfig) {

        this.name = name;
        this.clientMethod = clientMethod;
        this.usingDateRange = usingDateRange;
        this.usesAnalytics = usesAnalytics;
        this.initCB = initCB;
        this.serviceCB = serviceCB;
        this.highchartsConfig = highchartsConfig;

        this.$chart = $('#' + name);
        this.isIncluded = false;

        this.highchartsConfig.chart.renderTo = this.name;

        this.initCB && this.initCB(this);
    };
};

Look at module.value() . module.value() From doc: 从文档:

Register a value service with the $injector, such as a string, a number, an array, an object or a function. 向$ injector注册一个值服务,例如字符串,数字,数组,对象或函数。 This is short for registering a service where its provider's $get property is a factory function that takes no arguments and returns the value service. 这是注册服务的缩写,该服务的提供者的$ get属性是一个不带任何参数并返回值service的工厂函数。

UPDATE UPDATE

Not sure if you are still interested in it, but I finally found a correct answer to your question - make use of the $controller service. 不知道您是否仍然对此感兴趣,但是我终于找到了您问题的正确答案-使用$controller服务。 HERE I created a simple demo: 在这里,我创建了一个简单的演示:

app.controller('MainCtrl', function($scope, $controller) {

  function Person($interpolate, name, surname){
    var greetStr = "Hello {{name + ' ' + surname}}!";
    var greetExp = $interpolate(greetStr);
    this.greet = greetExp({name:name, surname:surname});
  }

  $scope.person1 = $controller(Person, {name:"Jack", surname:"Daniels"});
  $scope.person2 = $controller(Person, {name:"Johny", surname:"Walker"});
});

(I used $interpolate just to show that angular's DI will automatically resolve any dependencies it is aware of. The rest of dependencies [locals] may be passed as the second argument to the $controller .) (我使用$interpolate 只是为了表明angular的DI将自动解决它所知道的所有依赖项。其余的依赖项[locals]可以作为第二个参数传递给$controller 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM