2016-11-08 28 views
-2

大家好,我正嘗試使用角度js將字段值之一從一個html頁面攜帶到另一個html頁面。下面是代碼片段無法使用角度js將表格字段數據攜帶到下一個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頁面

我在哪裏做錯了,請幫我出

謝謝 Mark。

+0

您的問題需要澄清。當你說「兩個不同的html頁面」時,你是使用角度路由('http://whatever.com /#/ route1','http://whatever.com /#/ route2')還是常規路由('http :// whatever.com/page1 /','http:// whatever.com/page2 /')? –

+0

定期路由 – mark

+0

可能與此主題相同: http://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers – Basilf

回答

1

從您在評論中指定的內容看來,您似乎是在實際頁面之間進行導航而不是AngularJS頁面。

當你這樣做時,你的瀏覽器基本上放棄了一切,並從服務器請求新的頁面(過度簡化但仍然有效)。這意味着它也會放棄Angular應用程序的當前狀態並在新頁面上重新創建它。

爲了避免這種情況,您應該開始使用Angular路由,並將需要Angular的所有內容保存到單頁應用程序(SPA)中。

我建議使用UI的路由器,我覺得它非常可用: https://github.com/angular-ui/ui-router

相關問題