将数据从一条路线传递到另一条路线

时间:2019-05-04 18:09:09

标签: angularjs

它必须能够在路由控制器之间传递数据。例如,在一条路线中有一个输入字段,其内容将在下一条路线中显示。我不需要$rootScope

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = "John Doe";
});
</script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
<h1>You entered: {{name}}</h1>

<a href="">Click to see this data to another page (route)</a>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

查看此内容

RipTutorial - Handling session storage through service using angularjs

app.factory('storageService', ['$rootScope', function($rootScope) {

    return {
        get: function(key) {
            return sessionStorage.getItem(key);
        },
        save: function(key, data) {
            sessionStorage.setItem(key, data);
        }
    };
}]);

app.controller('myCtrl',['storageService',function(storageService) {

  // Save session data to storageService
  storageService.save('key', 'value');

  // Get saved session data from storageService
  var sessionData = storageService.get('key');

});