我正在创建一个名为Popup Service的共享服务,以便我可以在两个控制器之间共享数据,即BankController和CreateBankController。
我能够在PopupService中设置我需要共享的对象。在BankController中调用popupService(同时单击数据行上的Edit链接)以设置要共享的对象。
然后弹出窗体调用CreateBankController,但是在CreateBankcontroller中我无法访问PopupService的共享数据,我收到一个错误,即没有定义objectToEdit变量。
工作PLUNKER https://plnkr.co/edit/y8dZNU?p=preview
在CreateBankController中检索数据
CreateBankController.$inject = ['PopupService'];
function CreateBankController(PopupService) {
var vm = this;
var data = {
bankName: "",
bankAddress: "",
};
debugger;
if (PopupService.getObjectToEdit() !== null) {
data = PopupService.getObjectToEdit();
}
在BankController中设置共享数据
$scope.bankGrid = {
dataSource: queryResult,
columns: [{
dataField: 'orderID',
caption: 'Order ID'
}, {
width: 50,
alignment: 'center',
caption: 'Edit',
cellTemplate: function(container, options) {
$('<a/>').addClass('dx-link')
.text('Edit')
.on('dxclick', function() {
PopupService.addObjecToEdit(options.data);
$scope.showPopup = true;
})
.appendTo(container);
}
共享数据服务 - POPUP服务
(function () {
'use strict';
angular
.module('myApp')
.factory('PopupService', PopupService);
function PopupService() {
var popupInstance = {};
var objectToEdit = {};
var service = {
addObjecToEdit : addObjecToEdit,
getObjectToEdit: getObjectToEdit,
showPopup: showPopup,
hidePopup: hidePopup
};
return service;
//Functions
function addObjecToEdit(pObjectToEdit){
objectToEdit = pObjectToEdit;
}
function getObjectToEdit() {
return objecTtoEdit;
}
function showPopup(){
popupInstance.showPopup();
}
function hidePopup(){
popupInstance.hidePopup();
}
}
}());
答案 0 :(得分:1)
您在服务中输错:
function getObjectToEdit() {
return objecTtoEdit;
}
更改为:
function getObjectToEdit() {
return objectToEdit;
}