0

我有一个具有以下基本结构的应用程序,

weatherDisplayController.js
  weatherGrabbingService.js
  userColorPreferencesService.js

当用户更改查看天气的颜色偏好时,它会存储在 userColorPreferencesService.js.

但是,我想添加另一个视图,您可以在其中查看所有朋友的仪表板,这意味着创建一个新的模块微实例。但是,当我这样做时,他们将覆盖服务中的颜色首选项。

如何在一页上拥有同一模块的多个实例?

4

2 回答 2

0

唯一变化的数据应该在页面的该部分的范围内。您可以将数据存储在每个作用域中并将所需的数据传递给服务,而不是将数据存储在服务中。

于 2014-04-10T19:26:59.700 回答
0

那是因为服务是单例的。它只有 1 个现有实例。

也许将您的 colorservice 中的构造函数作为 oop 类并将它们中的多个存储在服务本身中。

function ColorPreferences(){
  //any data
} 

app.service("userColorPreferencesService", function(){
  this.ownColors = new ColorPreferences({ /* data goes here */});
  this.buddyColors = [];
});

//in your controller, when sharing actived
userColorPreferencesService.buddyColor = new ColorPreferences({ /* data from ajax? */});
于 2014-04-10T19:21:38.030 回答