pradeep kumar

pradeep kumar

  • NA
  • 32
  • 7.7k

Passing value from controller to controller

Sep 23 2017 2:53 PM
I trying to Pass value from one controller to another controller using Factory,
 
I am trying based on this example http://www.c-sharpcorner.com/article/pass-data-from-one-page-to-other-using-factory-or-service-in-angularjs/
 
Below is the code im using
 
Html code:
  1. <div ng-app="myApp" ng-controller="Phy1Controller">  
  2. <table class="wtable">  
  3. <tr>  
  4. <th colspan="6" class="colspanth">Performance</th>  
  5. </tr>  
  6. <tr>  
  7. <th class="wth">  
  8. Physician  
  9. </th>  
  10. <th class="wth">  
  11. Brand Trx  
  12. </th>  
  13. <th class="wth">  
  14. % Brand Trx Chg  
  15. </th>  
  16. <th class="wth">  
  17. Brand Mkt shr  
  18. </th>  
  19. <th class="wth">  
  20. brand Market shr Chg  
  21. </th>  
  22. <th class="wth">  
  23. Mkt Volume  
  24. </th>  
  25. </tr>  
  26. <tr ng-repeat="tab in Phyperform">  
  27. <td class="wtd"><a href="#" ng-click="open(tab.Physician_nm)"> {{tab.Physician_nm}}</a> </td>  
  28. <td class="wtd"> {{tab.Brand_trx}} </td>  
  29. <td class="wtd"> {{tab.Brand_trx_chng}} </td>  
  30. <td class="wtd"> {{tab.Brand_mkt_shr}} </td>  
  31. <td class="wtd"> {{tab.Brand_mkt_shr_chng}} </td>  
  32. <td class="wtd"> {{tab.Mkt_volume}} </td>  
  33. </tr>  
  34. </table>  
  35. </div>  
  36. var app = angular.module("myApp", ['ui.router']);  
  37. app.config(function ($stateProvider, $urlRouterProvider) {  
  38. $urlRouterProvider.otherwise('/home');  
  39. $stateProvider  
  40. .state('ShowData', {  
  41. url: '/ShowData',  
  42. templateUrl: '../Pages/Show.html'  
  43. })  
  44. .state('EditData', {  
  45. url: '/EditData',  
  46. controller:'Editctrl',  
  47. templateUrl: '../Pages/Edit.html'  
  48. })  
  49. });  
  50. app.factory("Phyfactory"function () {  
  51. var savedData = {};  
  52. function set(data) {  
  53. savedData = data;  
  54. }  
  55. function get() {  
  56. return savedData;  
  57. }  
  58. return {  
  59. set: set, get: get  
  60. }  
  61. })  
  62. app.controller("myCtrl"function ($location) {  
  63. alert('my')  
  64. $location.path('/Showdata')  
  65. });  
  66. //app.controller("PhyController", function ($location) {  
  67. // alert('hi');  
  68. // //$location.path('/Showdata')  
  69. //})  
  70. app.controller('Phy1Controller'function ($scope, $http, $location,Phyfactory) {  
  71. alert('WTF');  
  72. $scope.Phyperform = [];  
  73. $http.get('/Home/Get_Physician_Performance').then(function (d) {  
  74. $scope.Phyperform = d.data;  
  75. alert(JSON.stringify(d.data));  
  76. $scope.open = function (name) {  
  77. var message = name;  
  78. console.log('Hcpname', message);  
  79. Phyfactory.set(message);  
  80. $location.url('/EditData');  
  81. //window.location='/Htmlpages/Hcp_performance.html';  
  82. }  
  83. }, function (error) {  
  84. alert('Failed');  
  85. });  
  86. });  
  87. app.controller('Editctrl'function ($scope, $location, Phyfactory) {  
  88. //Phyfactory.get();  
  89. $scope.stud = Phyfactory.get();  
  90. })  
On ng-click its not redirecting to another page and i need to pass the value from Phy1Controller to Editctrl as well.
 
Quick help please

Answers (3)