当前位置: > > > > 为什么我的 go 服务说我的“项目”不是从我的服务器定义的?
为什么我的 go 服务说我的“项目”不是从我的服务器定义的?
来源:stackoverflow
2024-04-30 16:42:37
0浏览
收藏
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《为什么我的 go 服务说我的“项目”不是从我的服务器定义的?》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~
问题内容
我运行我的go服务,然后出现这个错误,我不知道如何解决它(恐慌:模板:index.html:20:函数“item”未定义)它应该是针对我的html的但我该如何解决它:
html 代码(角度)
<table id="mytable" class="table table-bordred table-striped">
<tr>
<th>id</th>
<th>name</th>
<th>last name</th>
<th>second last name</th>
</tr>
<tbody>
<tr ng-repeat="item in listofusers">
<td ng-if="listofusers.length!==0">{{item._id}}</td>
<td ng-if="listofusers.length!==0">{{item._name}}</td>
<td ng-if="listofusers.length!==0">{{item._lastname}}</td>
<td><a class="btn btn-primary btn-xs" href="#!edituser/{{item.id}}">
<span class="glyphicon glyphicon-pencil"></span></a>
</td>
<td><a class="btn btn-danger btn-xs"ref="#!deleteuser/{{item.id}}">
<span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
</tbody>
</table>
去服务
func main(){
fs := http.FileServer(http.Dir("Resources"))
go print10000numbers("world")
print10000numbers("hello")
router := mux.NewRouter()
router.HandleFunc("/People",GetPeopleHandler).Methods("GET")
router.HandleFunc("/InsertPeople",InsertPersonHandler).Methods("POST")
router.HandleFunc("/UpdatePeople/{id}",UpdatePersonHandler).Methods("POST")
router.HandleFunc("/DeletePeople/{id}",DeletePersonHandler).Methods("DELETE")
router.Handle("/Resources/Angular/angular.js",http.StripPrefix("/Resources", fs))
router.Handle("/Resources/JS/AppController.js",http.StripPrefix("/Resources", fs))
tpl = template.Must(template.ParseGlob("Views/*"))
fmt.Println("Listening")
router.HandleFunc("/",chargeHtml).Methods("GET")
http.ListenAndServe(":8081",router)
}
func chargeHtml(w http.ResponseWriter,r *http.Request){
tpl.ExecuteTemplate(w,"index.html",nil)
}
解决方案
Go 模板解析器找到了 Angular 表达式并想要解释它们。它没有找到名为 item 的函数。
您需要应用其中一种解决方案
- 在模板中使用字符串
{{`{{Your.Angular.Data}}`}} - 使用
{{"{{"}},{{"}}"}}转义表达式 - 使用不同的
tpl = template.Must(template.ParseGlob("Views/*").Delims("<(",")>"))
在您的情况下,您不使用任何模板功能,因此您可以删除它们。
来源
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持米云!更多关于Golang的相关知识,也可关注米云公众号。
