【问题标题】:Pass data between html pages using angular使用角度在html页面之间传递数据
【发布时间】:2017-05-01 20:08:11
【问题描述】:

我正在尝试在不使用服务的情况下在 html 页面之间传递表单数据。

这是我的代码链接 --> http://plnkr.co/edit/E6LM5Oq67XRcvVuMIv9i?p=preview 下面是我的contacts.html页面。

 <html>
<head>
<title>Contacts Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.js"></script>
 <script src="app.js"></script>
</head>
<body ng-app="myApp" >
<table>
<tr>
<th>User ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
</tr>
<tr ng-repeat="contact in contacts">
<td>{{contact.uid}}</td>
<td>{{contact.fname}}</td>
<td>{{contact.lname}}</td>
<td>{{contact.pphone}}</td>
</tr>
</table>
</body>
</html>

数据没有传递给contacts.html。如何做到这一点?

【问题讨论】:

  • 请在您的问题中包含所有相关信息(包括代码)。看看How to Askminimal reproducible example
  • 贴出代码
  • 两者都是不同的 HTML 页面。你不能这样传递数据。
  • 我可以在同一个 html 页面中显示数据,但还有其他方式在另一个页面中显示数据吗?
  • 您可以将数据作为查询字符串参数传递到不同的页面

标签: javascript html angularjs


【解决方案1】:

使用 rootScope 我们可以归档它。

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $rootScope) {
    $scope.carname = "Volvo";
    $rootScope.car=$scope.carname;
});
app.controller('controllerTwo', function($scope, $rootScope) {
    $scope.getcarname = $rootScope.car;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">

<h1>{{carname}}</h1>



<div ng-controller="controllerTwo">

<h1>Using rootscope: {{getcarname}}</h1>

</div>
  </div>

【讨论】:

  • 这不是$rootScope 的预期目的;虽然这可能有效,但传授这样的不良做法并不是一个好主意。
  • 另外,无论如何这在这个问题的情况下都不起作用,因为即使$rootScope 也只能在单个角度应用程序中工作,并且这里的示例有两个单独的 HTML 页面,具有独特的角度每个应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多