为什么我的go服务说未从服务器定义我的“项目”?

时间:2018-11-06 09:25:27

标签: angularjs 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) 
}

1 个答案:

答案 0 :(得分:3)

Go模板解析器找到了Angular表达式,并希望对其进行解释。它没有找到名为item的函数。

您需要应用一种解决方案

  1. 在模板{{`{{Your.Angular.Data}}`}}中使用字符串
  2. 使用{{"{{"}}{{"}}"}}
  3. 转义表达式
  4. 使用其他delimiters

tpl = template.Must(template.ParseGlob("Views/*").Delims("<(",")>"))

在您的情况下,您不使用任何模板功能,因此可以将其删除。

来源How do I escape “{{” and “}}” delimiters in Go templates?