我可以设置动态在服务中创建一个嵌套的对象内的服务this.'something“? (也许范围的问题) [英] Can I set this.'something' of a service within a nested object that is dynamically created in that service? (maybe scope issues)

查看:178
本文介绍了我可以设置动态在服务中创建一个嵌套的对象内的服务this.'something“? (也许范围的问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下服务(它实际上有更多的层次和内容,但我修剪下来到想什么,我的工作只是一个例子)。它创建动态的基于app.data文件夹CSV文件,其它对象中的JavaScript对象。最相关的这个问题,该生产线是 this.images =图像; 后最内层的for循环

I have the following service (it actually has more levels and elements but I trimmed it down to just a single example of what I'd like to work). It creates javascript objects within other objects dynamically based on csv files in the app.data folder. The line most relevant to this question is this.images = images; after the inner-most for loop.

app.service('tabInfoService',function(csvService, tabsService ){


    var tabData = tabsService.tabData;
    var tabCount = tabsService.tabCount;
    this.tabs={};
    this.images={};

    var tCounter = 0;
    //for loop creates a big object literal with more object literals inside of it 
    for (tCounter=0; tCounter<tabCount; tCounter++){
        var tabImageURL = "Contents/Product Groups/"+tabData[tCounter]+"/imageOrder.csv";
        var TabImages = $.getCsvArray(tabImageURL); //gets array from csv file
        var images={};

        this.tabs["tab"+tCounter] ={
            title: tabData[tCounter],  

            images : (function(){
                                        for (imageCounter = 0; imageCounter<TabImages.length; imageCounter++)                                                   
                                        images["image"+imageCounter]    ={
                                            image: "Contents/Product Groups/"+tabData[tCounter]+"/img/"+TabImages[imageCounter]

                                        };
                                        this.images=images; //can I make something like this so that I'll have access in my controller?
                                        return images;
                            })(),

        }
    }   
});

我怎样才能让像 this.images =图像; 嵌套JavaScript对象内工作,这样我可以访问此图片在我的控制器对象的东西,如 $ scope.images = tabInfoService.images

How can I make something like this.images = images; work inside the nested javascript objects so that I can access this images object in my controller with something like $scope.images = tabInfoService.images ?

目前,我可以在我的控制器做 $ scope.navItems = tabInfoService.tabs ,并有机会获得大对象作为一个整体。这让我窝 NG-重复(somethingElse的东西内navItems重复的东西,然后)的div,以重复这一切的元素图片对象。

Currently I can do $scope.navItems = tabInfoService.tabs in my controller and have access to the large object as a whole. This allows me to nest ng-repeat divs (repeating something in navItems, and then somethingElse within that something) in order to repeat all the elements of this images object.

所以基本上,我可以访问 tabInfoService.tabs 对象,并深入到它获得通过嵌套NG-重复我想要的图像的HTML。该应用程序正常工作与此,但我有问题,则初始化控制器中的数据。我可以设置一个新的 this.something (在这种情况下, this.images )的 tabInfoService 这样我可以在$范围变量设置为呢?

So basically, I can access the tabInfoService.tabs object and dig into it to get the images I want by nesting ng-repeats in the HTML. The app works fine with this, but I'm having issues initializing the data in the controller. Can I set a new this.something (in this case this.images) for tabInfoService so I can set a $scope variable to it?

长话短说:
我想要做的 $ scope.initialImage = tabInfoService.images [图片0] ,但我不能设置 tabInfoService.images 在我的服务嵌套对象之内。没有办法做到这一点?

Long story short: I want to do $scope.initialImage=tabInfoService.images[image0] but I can't set tabInfoService.images within the nested object in my service. Any way to do this?

我应该移到这一切给控制器?

Should I move this all to the controller?

非常感谢您的宝贵时间。我真的AP preciate它。

Thank you very much for your time. I really appreciate it.

推荐答案

您会希望对象的深层副本。一种选择是使用 angular.copy

You'll want to make a deep copy of the object. One option is to use angular.copy.

angular.copy(images, this.images);

例如plnkr - 见行36对37

example plnkr - see lines 36 vs 37.

这篇关于我可以设置动态在服务中创建一个嵌套的对象内的服务this.'something“? (也许范围的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆