2016-12-24 92 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變量。它需要在每個摘要循環中從工廠獲取值。

相關問題