Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将对象从一个控制器发送到另一个控制器?_Javascript_Jquery_Angularjs - Fatal编程技术网

Javascript 如何将对象从一个控制器发送到另一个控制器?

Javascript 如何将对象从一个控制器发送到另一个控制器?,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,您能告诉我如何将一个控制器中的对象以角度发送到另一个控制器吗?我可以通过发送参数来完成。是否有其他方法将对象发送到另一个控制器中 我确实喜欢这个 按一下按钮,我会这样做。数据是对象。用户id是字符串 $location.path('/navigation/'+JSON.stringify(data)+"/"+$scope.userId); 配置中 我就是这样 是否有其他方法可以在不使用本地存储的情况下在另一个视图中发送对象 $scope.checkPerson=function(){

您能告诉我如何将一个控制器中的对象以角度发送到另一个控制器吗?我可以通过发送参数来完成。是否有其他方法将对象发送到另一个控制器中

我确实喜欢这个 按一下按钮,我会这样做。数据是对象。用户id是字符串

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

我就是这样

是否有其他方法可以在不使用本地存储的情况下在另一个视图中发送对象

$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;
        });
}
如何与服务共享? 有什么想法吗?

角度服务是单例的。由于这个原因,它们是最重要的


作用域提供了一个事件系统,可以在父作用域和子作用域之间传递数据。它比服务更高级。

使用服务很容易实现:运行示例

HTML


考虑使用服务。Angular Services非常适合在应用程序中的控制器之间共享数据。您如何知道…请获取静态数据…不确定您的意思。让我详细说明我所说的话。服务的概念是,每个控制器中都可能需要这些服务。因此,您可以将数据发送到一个控制器中的服务。然后,该信息将在应用程序的整个状态中保留在服务中。然后,您可以将该服务注入另一个控制器并检索其数据。我将进行解释。请首先检查更新。实际上,单击按钮我调用webservice。并获取响应。该响应我需要发送另一个控制器。现在我发送参数,还有其他方法吗?
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;
        });
}
<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>
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;
});