以角度js将数据从一个页面传递到另一个页面

时间:2016-02-15 08:58:04

标签: angularjs node.js

如何在角度js中将数据从一个页面传递到另一个页面? 我听说过使用某些东西作为服务,但我不知道如何使用它! 以下是我想要执行的功能! 在第1页:

<div class="container" ng-controller="mycontrl">
  <label for="singleSelect"> Select Date </label><br>
  <select nAMe="singleSelect" ng-model="dateSelect">
    <option value="2/01/2015">2nd Jan</option>
    <option value="3/01/2015">3rd Jan</option>  
  </select>
  <br>
  Selected date = {{dateSelect}}
  <br/><br/><br/>
  <label for="singleSelect"> Select time </label><br>
  <select nAMe="singleSelect" ng-model="timeSelect">
    <option value="9/10">9AM-10AM</option>
    <option value="10/11">10AM-11AM</option>
    <option value="11/12">11AM-12PM</option>
    <option value="12/13">12PM-1PM</option>
    <option value="13/14">1PM-2PM</option>
  </select>
  <button ng-click="check()">Check!</button>
  <br>
  Selected Time = {{timeSelect}}
  <br/><br/>

用户选择时间和日期,用于调用db,结果存储在变量数组中! 第1页控制器:

var config= {
  params: { 
    time: times,
    date:dates
  }
};

$http.get('/era',config).success(function(response) {
  console.log("I got the data I requested");
  $scope.therapist_list = response;
});

现在我如何将这个变量$scope.therapist_list作为一个数组发送到下一页,它将拥有一个不同的控制器,如果使用了服务,如何在我的application.js文件中定义它

application.js:

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

2 个答案:

答案 0 :(得分:6)

要将要传输的数据保存到另一个$scope或在路由期间保存,您可以使用这些服务(Service)。由于servicesingleton,因此您可以使用它来存储和共享数据。

查看jsfiddle的示例。

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


myApp.controller("ExampleOneController", function($scope, NewsService) {
  $scope.news = NewsService.news;
});
myApp.controller("ExampleTwoController", function($scope,NewsService) {
  $scope.news = NewsService.news;
});

myApp.service("NewsService", function() {
  return {
    news: [{theme:"This is one new"}, {theme:"This is two new"}, {theme:"This is three new"}]
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <div ng-controller="ExampleOneController">
    <h2>
  ExampleOneController
  </h2>
    <div ng-repeat="n in news">
      <textarea ng-model="n.theme"></textarea>
    </div>
  </div>
  <div ng-controller="ExampleTwoController">
    <h2>
  ExampleTwoController
  </h2>
  <div ng-repeat="n in news">
      <div>{{n.theme}}</div>
    </div>
  </div>
</body>

<强>已更新

在不同的控制器jsfiddle中显示使用变量。

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


myApp.controller("ExampleOneController", function($scope, NewsService) {
  $scope.newVar = {
    val: ""
  };
  NewsService.newVar = $scope.newVar;
  $scope.news = NewsService.news;
});
myApp.controller("ExampleTwoController", function($scope, NewsService) {
  $scope.anotherVar = NewsService.newVar;
  $scope.news = NewsService.news;
});

myApp.service("NewsService", function() {
  return {
    news: [{
      theme: "This is one new"
    }, {
      theme: "This is two new"
    }, {
      theme: "This is three new"
    }]
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <div ng-controller="ExampleOneController">
    <h2>
  ExampleOneController
  </h2>
    <div ng-repeat="n in news">
      <textarea ng-model="n.theme"></textarea>
    </div>
    <input ng-model="newVar.val">
  </div>
  <div ng-controller="ExampleTwoController">
    <h2>
  ExampleTwoController
  </h2>
    <div ng-repeat="n in news">
      <div>{{n.theme}}</div>
    </div>
    <pre>newVar from ExampleOneController {{anotherVar.val}}</pre>
  </div>
</body>

答案 1 :(得分:0)

行, 你用工厂服务写了另一个模块,例如

app.controller('Ctrl1', function ($scope, share) {
    $scope.variable1 = "One";
    share.setVariable('variable1', $scope.variable1); //setting value in service variable
});

app.controller('Ctrl2', function ($scope, share) {
    console.log("shared variable " + share.getSharedVariables());
});

将此模块作为依赖项添加到您的两个页面应用程序,如

angular
    .module('dataApp')
    .factory('dataService', factory);

factory.$inject = ['$http', '$rootScope', '$q', '$log'];

function factory($http, $rootScope,$q,$log) {
    var service = {
        list: getList
    };
    service.therapist_list = null;
    return service;


    function getList() {
        var config= {
            params: { 
                time: times,
                date:dates
            }
        };

        $http.get('/era',config).success(function(response) {
        console.log("I got the data I requested");
        $scope.therapist_list = response;

        });

        $log.debug("get Students service");
        $http.get('/era',config).then(function successCallback(response) {
            service.therapist_list = response.data;
            $log.debug(response.data);
        }, function errorCallback(response) {
            $log.debug("error" + response);
        });
    }
}

然后在你的控制器中使用该服务

var firstpage=angular.module('firstpage', [dataApp]);
var secondpage=angular.module('secondpage', [dataApp]);