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

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

问题描述

我有以下服务(它实际上有更多级别和元素,但我将其缩减为我想要工作的一个示例).它根据 app.data 文件夹中的 csv 文件在其他对象中动态创建 javascript 对象.与这个问题最相关的一行是 this.images = images; 在最里面的 for 循环之后.

app.service('tabInfoService',function(csvService, tabsService){var tabData = tabsService.tabData;var tabCount = tabsService.tabCount;this.tabs={};this.images={};var tCounter = 0;//for 循环创建一个大对象字面量,其中包含更多对象字面量for (tCounter=0; tCounter

我怎样才能让 this.images = images; 之类的东西在嵌套的 javascript 对象中工作,以便我可以在我的控制器中使用类似 $scope.images = 的东西访问这个图像对象tabInfoService.images ?

目前我可以在我的控制器中执行 $scope.navItems = tabInfoService.tabs 并且可以访问整个大对象.这允许我嵌套 ng-repeat div(在 navItems 中重复某些内容,然后在该内容中重复 somethingElse)以重复此 images 对象的所有元素.

所以基本上,我可以访问 tabInfoService.tabs 对象并深入研究它以通过在 HTML 中嵌套 ng-repeat 来获取我想要的图像.该应用程序可以正常工作,但我在初始化控制器中的数据时遇到问题.我可以为 tabInfoService 设置一个新的 this.something(在本例中为 this.images),以便为它设置一个 $scope 变量吗?

长话短说:我想做 $scope.initialImage=tabInfoService.images[image0] 但我无法在我的服务的嵌套对象中设置 tabInfoService.images.有什么办法吗?

我应该把这些都移到控制器上吗?

非常感谢您抽出宝贵时间.我真的很感激.

解决方案

您需要制作对象的深层副本.一种选择是使用 angular.copy.

angular.copy(images, this.images);

示例 plnkr - 参见第 36 行和第 37 行.

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;
                            })(),

        }
    }   
});

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 ?

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.

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?

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?

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

解决方案

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

angular.copy(images, this.images);

example plnkr - see lines 36 vs 37.

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

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