【发布时间】:2014-11-26 06:29:20
【问题描述】:
希望,我的问题本身,传达了我正在寻找的东西。 会把话详细说 1. 创建模块。
var ang = angular.module('myApp', []);
- 我有一个名为 controller1 的控制器,其中包括“活动”工厂。
//controllerone.js
ang.controller('controller1', function(campaign){ $scope.campaigns = new campaign(); //Here the whole campaign object is displayed with data, refer the Image 1 attached console.log($scope.campaigns); });
ang.factory('campaign', function($http){
var campaign = function(){
this.timePeriodList = buildTimePeriodList();
...
...
this.campaignList = [];
};
Campaigns.prototype.fetchCampaigns = function() {
//Some service call to load the data in this.campaignList
};
});
现在尝试在第二个控制器中调用相同的活动工厂,只获取对象结构,而不是获取数据。
//controlertwo.js
ang.controller('controller2', function(campaign){ $scope.campaigns = new campaign(); //Here only the campaign object structure is displayed, but no data for campaignList, ref image 2 attached console.log($scope.campaigns); });
因为,工厂服务是一个单例对象,我期望得到与我在 controllerone.js 中得到的结果相同的结果,
图片1:
图片2:
【问题讨论】:
-
如果你想利用单例,请停止使用
new。
标签: javascript angularjs prototype-programming