我正在建造一个带有棱角和纤细框架的水疗中心。根据下面提到的代码,我想要做的是,登录页面控制器将成功提交user / psw后将数据传递给登陆页面控制器。当我将工厂放在http呼叫/登录功能之外时,它会获取数据,但在登陆页面工厂不提供数据。当我把它放在里面停止工作。请帮帮我......
此工厂用于跨控制器共享数据
appDls.factory('sharedFactory', function () {
var dataTobeShared = {};
var interface = {};
interface.add = function (d) {
dataTobeShared = d;
}
interface.put = function () {
return dataTobeShared;
}
return interface;
});
此控制器用于主门户网站用户重定向和门户网站呈现
appDls.controller('DlsappController', ['$scope', '$state', 'multipartForm', 'sharedFactory', '$window', function ($scope, $state, multipartForm, sharedFactory, $window) {
$scope.Userdata = [];
$scope.login = function () {
var url = "../scripts/routes.php/authen";
multipartForm.post(url, $scope.login).then(function (d) {
$scope.Userdata.push(d.data[0]);
sharedFactory.add($scope.Userdata);
$window.location.href = '../portal/landing.php';
});
}
}]);
此控制器用于着陆页路由
appDls.controller('landingController', ['$scope', '$state', 'multipartForm', 'sharedFactory', function ($scope, $state, multipartForm, sharedFactory) {
$scope.UserInfo = sharedFactory.put();
$scope.$watch('UserInfo', function (newValue, oldValue) {
/*here we can use the user data from login page*/
if (newValue.length == 1) {
alert(newValue[0].fullname);
$state.go(newValue[0].proftype);
} else {
alert("user not logged in successfully!");
$state.go('default');
}
}, true);
}]);
答案 0 :(得分:0)
当您执行sharedFactory.add($scope.Userdata);
$scope.Userdata
时,landingController
是另一个dataToBeShared
未观看的对象。通过在sharedFactory.add
函数中重新分配landingController
,您将丢失对原始对象的引用,因此无法再从代码中访问它。
要使sharedFactory.add
看到更改,您需要重新实现sharedFactory.dataTobeShared
功能以推送 lapply(1:length(markers), function(i)
{
newNames[i] <<- input[[paste0("marker", i)]]
})
printf(newNames)
数组中的值或使用某些基于事件的通知,而不是$手表。
以下jsfiddle来说明我的话。
答案 1 :(得分:0)
appDls.factory('sharedFactory', function () {
var dataTobeShared = {};
return
{
add: function (d) {
dataTobeShared = d;
}
put: function () {
return dataTobeShared;
}
}
});
appDls.controller('DlsappController', ['$scope', '$state', 'multipartForm', 'sharedFactory', '$window', function ($scope, $state, multipartForm, sharedFactory, $window) {
$scope.Userdata = [];
$scope.$watch('login',function () {
var url = "../scripts/routes.php/authen";
multipartForm.post(url, $scope.login).then(function (d) {
$scope.Userdata.push(d.data[0]);
sharedFactory.add($scope.Userdata);
$window.location.href = '../portal/landing.php';
});
}
}]);
appDls.controller('landingController', ['$scope', '$state', 'multipartForm', 'sharedFactory', function ($scope, $state, multipartForm, sharedFactory) {
$scope.UserInfo = sharedFactory.put();
$scope.$watch('UserInfo', function (newValue, oldValue) {
/*here we can use the user data from login page*/
if (newValue.length == 1) {
alert(newValue[0].fullname);
$state.go(newValue[0].proftype);
} else {
alert("user not logged in successfully!");
$state.go('default');
}
}, true);
}]);
答案 2 :(得分:0)
观察者需要在每个摘要周期从工厂获取值,并更新$ scope变量。
appDls.controller('landingController', ['$scope', '$state', 'multipartForm', 'sharedFactory', function ($scope, $state, multipartForm, sharedFactory) {
//$scope.UserInfo = sharedFactory.put();
//$scope.$watch('UserInfo', function (newValue, oldValue) {
$scope.$watch(sharedFactory.put, function (newValue, oldValue) {
//UPDATE scope variable
$scope.UserInfo = newValue;
/*here we can use the user data from login page*/
if (newValue.length == 1) {
alert(newValue[0].fullname);
$state.go(newValue[0].proftype);
} else {
alert("user not logged in successfully!");
$state.go('default');
}
}, true);
}]);
原始代码仅在控制器初始化时设置范围变量一次。它需要在每个摘要周期中从工厂获取值。