所以基本上,我可以访问 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' 吗?(可能是范围问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 01:37