2014-10-28 25 views
0

你能告訴我如何發送對象一個控制器到另一個控制器的角度?我能通過發送參數.is有辦法發送對象在另一個控制器嗎?如何將角色中的對象一個控制器發送到另一個控制器?

我喜歡這個 on button click我這樣做.data is object.user id is string。

$location.path('/navigation/'+JSON.stringify(data)+"/"+$scope.userId); 

在配置

when('/navigation/:userDetail/:voterID',{ 
      templateUrl: 'partial/userdetail.html', 
      controller: 'userdetail' 
     }) 

我找回這樣的

console.log(JSON.parse($routeParams.userDetail)); 

有沒有另一種方式使用本地存儲,發送與出另一個視圖對象

$scope.checkPerson=function(){ 
    $scope.loading=true; 
    $http.post('http://192.168.11.218:8082/onlinevote/rss/checkUserDetail', { 
     "voterID":$scope.userId, 
     "name":$scope.userName, 
     "fatherName":$scope.fatherName, 
     "territory":$scope.territory, 
     "dOB":$scope.dOB 
    }). 
     success(function(data, status, headers, config) { 
      // this callback will be called asynchronously 
      // when the response is available 
      console.log(data); 
      console.log(status); 
      console.log(headers); 
      console.log(config); 
      $scope.loading=false; 
      $scope.loading=false; 
      if(data.msg!="Already Vote with this id!") 
      $location.path('/navigation/'+JSON.stringify(data)+"/"+$scope.userId) 
      alert(data.msg) 
     }). 
     error(function(data, status, headers, config) { 
      // called asynchronously if an error occurs 
      // or server returns response with an error status. 
      alert('error') 
      $scope.loading=false; 
     }); 
} 

如何與服務共享? 有沒有任何想法?

+2

考慮使用絲氨酸副。 Angular Services非常適合在應用程序中的控制器之間共享數據。 – user2808895 2014-10-28 07:41:45

+0

你有什麼想法...請採取靜態數據... – user944513 2014-10-28 07:48:27

+0

不知道你的意思。讓我詳細說說我在說什麼。服務的想法是,他們可以在每個控制器中需要。因此,您可以在一個控制器中將數據發送到服務。這些信息在應用程序的整個狀態中保留在服務中。然後,您可以將該服務注入另一個控制器並檢索其數據。 – user2808895 2014-10-28 07:51:18

回答

0

它很容易實現使用的服務:例如運行Plunker

HTML

<div ng-app="myApp"> 
<div ng-controller="Ctr1"> 
    <input type="text" ng-model="Data.FirstName"> 
    <input type="text" ng-model="Data.LastName"> 
    <br>First Name in Ctr1 : <strong>{{Data.FirstName}}</strong> 
    <br>Last Name in Ctr1: <strong>{{Data.LastName}}</strong> 
</div> 
<hr> 
<div ng-controller="Ctr2"> 
    <br>First Name shared in Ctr2 : <strong>{{Data.FirstName}}</strong> 
    <br>Last Name shared in Ctr2: <strong>{{Data.LastName}}</strong> 
</div> 

的JavaScript

var myApp = angular.module('myApp', []); 

myApp.factory('Data', function(){ 
    return { }; 
}); 

myApp.controller('Ctr1', function($scope, Data){ 
    $scope.Data = Data; 
}); 

myApp.controller('Ctr2', function($scope, Data){ 
    $scope.Data = Data; 
}); 
相關問題