最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to configure factory externally in AngularJS - Stack Overflow

programmeradmin0浏览0评论

Say I am building a C# application with AngularJS.

I want to set up configuration object that es from server side and basically inject that configuration into a factory. Where the factory resides in another .JS file.

How would go about doing that?

I have a JS fiddle example set up here:

/

Say I am building a C# application with AngularJS.

I want to set up configuration object that es from server side and basically inject that configuration into a factory. Where the factory resides in another .JS file.

How would go about doing that?

I have a JS fiddle example set up here:

http://jsfiddle/f89tS/7/

Share Improve this question asked Aug 16, 2012 at 19:26 Chi ChanChi Chan 12.4k9 gold badges35 silver badges52 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

You could use module's constants for configuration objects ing from the server. Using constants is pretty easy, you could generate this on the server-side:

app.constant('CONSTANTS', {zoomLevel: 8});

and then, in your factory you can inject constants:

app.factory('map', function(CONSTANTS){
    return {
        zoomLevel: CONSTANTS.zoomLevel  
    };
});

Constants are really good for the server-generated settings since, once generated and sent to the client those can't change.

Finally, here is the working jsFiddle: http://jsfiddle/pkozlowski_opensource/JZcys/1/

Here is an example of how I acplished something similar by wrapping my bootstrap call around my own run method.

It then uses a naming convention to inject configuration options inline from your aspx page, which could be set via c# property.

I don't know if this is the 'angular' way, but it has worked well thus far.

http://jsfiddle/xpressivecode/dVM9b/

发布评论

评论列表(0)

  1. 暂无评论