Angularjs 工厂无法将数据传送到另一个控制器
我正在建造一个有棱角和纤细框架的水疗中心。根据下面提到的代码,我试图做的是,登录页面控制器将在成功提交用户/psw后将数据传递给登录页面控制器。当我将工厂放在http调用/登录函数之外时,它会获取数据,但在登录页上工厂不会传递数据。当我把它放进去的时候,它就停止工作了。请帮帮我 此工厂用于在控制器之间共享数据Angularjs 工厂无法将数据传送到另一个控制器,angularjs,angularjs-factory,Angularjs,Angularjs Factory,我正在建造一个有棱角和纤细框架的水疗中心。根据下面提到的代码,我试图做的是,登录页面控制器将在成功提交用户/psw后将数据传递给登录页面控制器。当我将工厂放在http调用/登录函数之外时,它会获取数据,但在登录页上工厂不会传递数据。当我把它放进去的时候,它就停止工作了。请帮帮我 此工厂用于在控制器之间共享数据 appDls.factory('sharedFactory', function () { var dataTobeShared = {}; var interface =
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);
}]);
此控制器用于登录页路由
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);
}]);
当您执行
sharedFactory.add($scope.Userdata)时
您的$scope.Userdata
是另一个对象,它不被landingController
监视。通过在sharedFactory.add
函数中重新分配dataToBeShared
,您将丢失对原始对象的引用,因此无法再从代码访问该对象
要使landingController
查看您需要的更改,或者重新实现sharedFactory。添加函数以推送sharedFactory.dataTobeShared
数组中的值,或者使用一些基于事件的通知,而不是$watch
下面是我的示例。观察者需要在每个摘要周期从工厂获取值,并更新$scope变量
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);
}]);
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);
}]);
原始代码仅在控制器初始化时设置范围变量一次。它需要在每个摘要周期从工厂获取值。请编辑您的帖子以停止叫喊-在任何地方使用这样的大写都是不礼貌的。谢谢您的评论…我已经编辑了问题。。。。