0
0

More than 1 year has passed since last update.

htmlのrange外で他の変数を参照する方法(Go)

Last updated at Posted at 2023-03-02

記事編集ページのセレクトバーはカテゴリテーブルのrangeで回していたが、
valueに編集する記事レコードのcategoryIdをselectedとしてどう表示するか。。

      <select class="form-select" name="categoryId" aria-label="Default select example">
        <option selected>カテゴリを選択</option>
        {{ range .Category }}
        {{ if eq .ID .Art.CategoryId }}
        <option value="{{ .ID }}" selected>{{ .Name }}</option>
      {{ else }}
        <option value="{{ .ID }}">{{ .Name }}</option>
      {{ end }}
        {{ end }}
      </select>

これだと
executing "content" at <.Art.CategoryId>: can't evaluate field Art in type models.Category
exit status 1
.Category rangeの中に.Art.CategoryIdなんてありません、と怒られますが。。

{{ if eq .ID .Art.CategoryId }}

 この部分を

{{ if eq .ID $.Art.CategoryId }}

 $をつけてあげることで、range外の変数を返せるようです.

0
0
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
0
0