大家好,我正在尝试使用angular js将其中一个字段值从一个html页面传送到另一个html页面。以下是代码片段
HTML:
<body ng-app="myApp">
<div ng-controller="ExampleOneController">
Name:<input type = "text" ng-model="newVar.val">
</div>
<div ng-controller="ExampleTwoController">
<pre>value of name is {{anotherVar.val}}</pre>
</div>
AngularJS:
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;
});
如果我将元素放在同一页面中,但如果我将它们放在两个不同的html页面中,则效果很好
我在哪里做错了。请帮帮我
谢谢马克。
根据您在注释中指定的内容,您似乎在实际页面之间导航,而不是在AngularJS页面之间导航。
执行此操作时,浏览器基本上会丢弃所有内容并从服务器请求新页面(极端简化,但仍然有效)。这意味着它还会删除Angular应用程序的当前状态,并在新页面上重新创建它。
为了避免这种情况,您应该开始使用Angular路由,并将所有需要Angular的内容都保留到单页应用程序(SPA)中。
我建议使用ui-router,因为我发现它非常有用:https://github.com/angular-ui/ui-router
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句