【发布时间】:2017-03-10 14:42:59
【问题描述】:
我正在尝试使用服务、工厂或广播在控制器之间传递数据的所有方法。它们都不适合我。我在网上遵循了确切的解决方案,但仍然很不幸。我将服务放在我的 app.js 中。
App.JS
myApp.service('customService', [function () {
this.list = [];
this.setObject = function (o) {
this.list.push(o);
},
this.getObject = function () {
return this.list;
}
}]);
控制器 #1
myApp.controller('Controller1', function ($scope, customService) {
customService.setObject({..});
$window.open("/controller2", '_blank');
}
控制器 #2
myApp.controller('Controller2', function ($scope, customService) {
console.log(customService.getObject()); // Returns []
}
问题
它从控制器 1 返回控制器 2 上的 [],而不是对象数据。
【问题讨论】:
-
与
getObject您想检索对象数组还是仅检索一个特定对象? -
previewService.getObject() 不会抛出异常?它与 customService 的名称不同
-
@Groben 应该同名。
-
@lealceldeiro 只是一个对象。但它没有返回任何东西..
标签: javascript angularjs service