LoginSignup
2
2

More than 5 years have passed since last update.

ステートが変わったらモーダルを強制的に閉じたい

Posted at

 

背景

  • AngularJSで画面を作っている
  • ui-routerを利用している
  • APIからの戻りがエラー(500番系)だったら、エラー専用画面に state.go('error_画面') で飛ばしている
  • 登録フォームなどはモーダルでポップアップさせている

こまった点

  • 上記のような環境で、モーダル画面経由でAPIにPOSTした
  • APIから500番エラーが返ってきて、一応 state.go('error_画面') に飛んでくれたところまでは想定内

でも、モーダルが閉じられず、ボタンを押してもモーダルが閉じてくれないという結果に...。
一画面だけならいいのですが、その他にも同じようにしている箇所があり、モーダルが特定できない。

でも、エラー専用画面に遷移したら、モーダルは閉じて欲しい。

対策 / $modalStack を利用

$modalStack.dismissAll() を使うと、モーダルを撤収してくれました。

以下、angular.module("アプリケーション").run(..) のところで差し込みした例。

CoffeeScript
if response.status => 500
   $modalStack.dismissAll()
   $state.go("error_画面")
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2