drq22639 2016-04-14 20:22
浏览 91
已采纳

GO HTML /模板:测试两个点变量是否相等

I'm sending an html/template this model:

type MapModel struct {
Networks      []*NetworkMeta
WaveKey       string

}

The Networks field is defined by another type, NetworkMeta:

type NetworkMeta struct {
NetworkMetaKey string

}

I use the Networks array to produce an html select object:

            <select name="waveKey" id="waveKey">
    {{range .Networks}}
            <option value="{{ .NetworkMetaKey}}" {{if eq .NetworkMetaKey .WaveKey }} selected="selected" {{end}}>
            {{ .NetworkMetaKey }}
            </option>
    {{end}}

Everything here works except the "if eq" equality test. That test returns the error: "WaveKey is not a field of struct type *models.NetworkMeta."

As I understand the html/template eq operator, the comparison tests one value against another (or a group of values), the one separated from the rest by a space. In this case, however, the error seems to indicate that for a field, the compiler ignores the space.

Is there any way to make this equality work? Do I need to write a custom func?

Thanks for any help.

  • 写回答

1条回答 默认 最新

  • drqj8605 2016-04-14 20:28
    关注

    dot is iterating through the slice of Networks, so it is of type *NetworkMeta. NetworkMeta doesn't have any fields of WaveKey.

    A custom func might be what you want since you are trying to access the values from different scopes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址