4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Angural JSとDjangoのテンプレートコンフリクトを解決する

Last updated at Posted at 2015-08-19

Webアプリを作る時に、バックエンドをPythonのDjango、フロントエンドをAngular JSだと、コンフリクトが起きてしまいます。しかも配列を使っている場合、一般的な方法も上手く行かず、ハマってしまいましたので書いておきます。

コンフリクトは何故起きる?

Djangoのテンプレートでは、{{}}をパラメータ挿入などに使いますが、Angular JSでも{{}}をマークアップの始めと終わりに使います。そのためコンフリクトが起きてしまうようです。

Angular JSのマークアップを別のもので行う

同じ{{}}を使うからコンフリクトしてしまうわけで、それに対して、Angular JS側のマークアップタグを別のものに変更する方法が用意されています。

<script>
var customInterpolationApp = angular.module('myapp', []);
  customInterpolationApp.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('//');
    $interpolateProvider.endSymbol('//');
  });

</script>

Appのconfigで

$interpolateProvider.startSymbol('//');

で開始タグを好きなものに、

$interpolateProvider.endSymbol('//');

で終了タグを好きなものに変えられます。
※ここでは、両方とも//に変更しています。

テンプレート中の{{}}も指定したタグ//に変更します。

公式ドキュメント

検索していると[[]]に変えている人が多いようでしたが、その場合、Angural JSの中で配列[]を使うと動かないようです。//にすることで、動くようになりました。

他に、Python側で対応する方法もあるようですが、この方法が私には簡単でしたので、共有しときます。

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?