2016-12-24 57 views
0

我正在建造一个有角度和纤细框架的spa。根据下面提到的代码,我试图做的是,成功提交用户/ 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); 

}]); 
+1

请编辑您的帖子停止呼喊 - 使用所有帽子这是非常粗鲁无处不在。 –

+0

感谢您的评论...我编辑过的问题.... –

回答

0

当你做你的sharedFactory.add($scope.Userdata);$scope.Userdata另一个对象,这是不被landingController观看。通过在sharedFactory.add函数中重新分配dataToBeShared函数,您将失去对原始对象的引用,因此无法再通过代码访问它。

为了landingController看看你需要要么重新实现sharedFactory.add功能推值sharedFactory.dataTobeShared阵列或使用一些基于事件的通知,而不是$关注这些变化。

这里是jsfiddle来说明我的话。

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); 

}]); 
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); 

}]); 

原始代码只在控制器初始化时设置一次scope变量。它需要在每个摘要循环中从工厂获取值。

相关问题