node.js 在角度js中将数据从一个页面传递到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35405070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:54:55  来源:igfitidea点击:

Passing data from one page to another in angular js

angularjsnode.js

提问by Mohammed Gadiwala

How do I pass data from one page to another in angular js? I have heard about using something as services but I am not sure how to use it! Given below is the functionality I want to execute! On page 1:

如何在angular 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/>

User selects time and date and that is used to make call to the db and results are stored in a variable array! Page 1 controller:

用户选择时间和日期,用于调用数据库,结果存储在变量数组中!第 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;
});

Now how do I send this variable $scope.therapist_listwhich is an array to next page which will be having a different controller and also if services is use how do define it in my application.js file

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

application.js:

应用程序.js:

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

回答by Stepan Kasyanenko

To save the data that you want to transfer to another $scopeor saved during the routing, you can use the services (Service). Since the serviceis a singleton, you can use it to store and share data.

要保存要传输到另一个$scope或在路由过程中保存的数据,您可以使用服务 ( Service)。由于service是 a singleton,您可以使用它来存储和共享数据。

Look at the example of jsfiddle.

看看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>

UPDATED

更新

Showing using variable in different controller jsfiddle.

在不同的控制器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>

回答by Shankar Gurav

OK, you write a another module ewith factory service e.g.

好的,您使用工厂服务编写另一个模块,例如

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);
        });
    }
}

Add this module as dependencies to your both page apps like

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

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

and then in your controller consume that service

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

.controller('homeController', ['$scope', 'dataService', function ($scope, dataService) {
}]);