2016-11-08 5 views
-2

こんにちはすべて私は角のjsを使用して、あるhtmlページから別のhtmlページにフィールド値のいずれかを運ぶことを試みています。角度jsを使用してフォームフィールドデータを次の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:以下のコードは

HTMLスニペットです私は同じページ内の要素を入れたが、私は中に入れない場合場合は正常に動作している

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

2つの異なるhtmlページ

私は間違っています。助けてください。

ありがとうございます マーク。

+0

あなたの質問は説明が必要です。あなたは "2つの異なるhtmlページ"と言うとき、角度ルーティング( 'http://whatever.com /#/ route1'、' http://whatever.com /#/ route2')、または通常のルーティング:// whatever.com/page1/'、' http:// whatever.com/page2/')? –

+0

通常のルーティング – mark

+0

おそらくこのスレッドと同じです: http://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers – Basilf

答えて

1

あなたのコメントで指定したところから、AngularJSページではなく実際のページ間を移動しているように見えます。

これを行うと、ブラウザは基本的にすべてを削除し、サーバーから新しいページを要求します(極端に単純化されていますが、依然として有効です)。つまり、Angularアプリの現在の状態を落として、新しいページに再作成します。

これを回避するには、角度ルーティングを使用して、単一ページアプリケーション(SPA)の角度を必要とするすべてのものを維持する必要があります。

私はそれが非常に使用可能な見つけるように私は、UI-ルータを使用することをお勧め: https://github.com/angular-ui/ui-router

関連する問題