かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

ng.ILocationServiceを使った画面遷移での画面間データ渡し

とりあえずパラメータを使うのが一番楽そうです。パラメータはng.ILocationServiceのsearchメソッドにオブジェクトを渡してやるといい感じに面倒を見てくれます。

前回の処理に、ページ間のデータ渡しをコントローラに追加したコードは以下のような感じになります。

// Page1用のスコープ
interface Page1Scope extends ng.IScope {
    title: string;
    navigate(): void;
}

// Page1用のコントローラ
class Page1Ctrl {
    constructor($scope: Page1Scope, $location: ng.ILocationService) {
        // タイトルと画面遷移を行う処理を定義する
        $scope.title = "Page1";
        $scope.navigate = () => {
            $location.path("/Page2").search({key: Date()});;
        };
    }
}

// Page1と基本同じなのでコメントは省略
interface Page2Scope extends ng.IScope {
    title: string;
    navigate(): void;
}

class Page2Ctrl {
    constructor($scope: Page2Scope, $location: ng.ILocationService) {
        $scope.title = "Page2 - " + $location.search().key;
        $scope.navigate = () => {
            $location.path("/Page1");
        }
    }
}