AngularJs在尝试在指令中访问数据时崩溃。

huangapple go评论138阅读模式
英文:

AngularJs crashes when trying to access data in directive

问题

所以我是Angular的新手,正在尝试创建一个应用程序。我的服务器非常基础,使用Go语言编写。这是我的Angular代码:

  1. (function() {
  2. var app = angular.module('dashboard', []);
  3. app.controller("JobsController", function() {
  4. this.currentJob = 0;
  5. this.jobs = jobs;
  6. this.setCurrentJob = function(index) {
  7. this.currentJob = index;
  8. };
  9. });
  10. var jobs = [
  11. {
  12. 'id': 1,
  13. 'requester': 'Prakash Sanker',
  14. 'description': '我想让你给我整个世界'
  15. },
  16. {
  17. 'id': 2,
  18. 'requester': 'MJ Watson',
  19. 'description': '请给我蜘蛛侠,最好是倒挂着...但我不挑剔'
  20. },
  21. {
  22. 'id': 3,
  23. 'requester': 'Josh Lyman',
  24. 'description': 'Matthew Santos当总统...或者Jed Bartlet...但请不要是共和党人'
  25. },
  26. {
  27. 'id': 4,
  28. 'requester': 'Barry Allen',
  29. 'description': '一套无摩擦的服装,当我以每小时数千英里的速度奔跑时不会起火...这是给一个朋友的'
  30. },
  31. {
  32. 'id': 5,
  33. 'requeter': 'A. Bambaata',
  34. 'description': '80年代的音乐播放器,状态良好。如果必要,可以倒回到过去'
  35. }
  36. ];
  37. })();
  38. 这是我的index.html
  39. ```html
  40. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  41. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  42. <script type="text/javascript" src="/dashboard.js"></script>
  43. <body>
  44. <div class="dashboard">
  45. <div ng-controller="JobsController as jobsCtrl">
  46. <div class="jobs">
  47. {{jobsCtrl.jobs[0]}}
  48. </div>
  49. </div>
  50. </div>
  51. </body>
  52. 这导致我的服务器出现以下错误:
  53. 2015/07/29 12:18:02 http: panic serving [::1]:56857: runtime error: invalid memory address or nil pointer dereference
  54. goroutine 18 [running]:
  55. net/http.func·009()
  56. /usr/local/go/src/pkg/net/http/server.go:1093 +0x9e
  57. runtime.panic(0x20ed40, 0x4efaed)
  58. /usr/local/go/src/pkg/runtime/panic.c:248 +0xda
  59. html/template.(*Template).escape(0x0, 0x0, 0x0)
  60. /usr/local/go/src/pkg/html/template/template.go:52 +0x30
  61. 为什么会发生这种情况?
  62. <details>
  63. <summary>英文:</summary>
  64. So I&#39;m new to Angular and trying to do an app. My server is very basic and written in go. This is my angular code-
  65. (function() {
  66. var app = angular.module(&#39;dashboard&#39;, []);
  67. app.controller(&quot;JobsController&quot;, function() {
  68. this.currentJob = 0;
  69. this.jobs = jobs;
  70. this.setCurrentJob = function(index) {
  71. this.currentJob = index;
  72. };
  73. });
  74. var jobs = [
  75. {
  76. &#39;id&#39;: 1,
  77. &#39;requester&#39;: &#39;Prakash Sanker&#39;,
  78. &#39;description&#39;: &#39;I want you to give me the entire world&#39;
  79. },
  80. {
  81. &#39;id&#39;: 2,
  82. &#39;requester&#39;: &#39;MJ Watson&#39;,
  83. &#39;description&#39;: &#39;Please give me Spiderman, preferably upside down...but Im not fussy&#39;
  84. },
  85. { &#39;id&#39;: 3,
  86. &#39;requester&#39;: &#39;Josh Lyman&#39;,
  87. &#39;description&#39;: &#39;Matthew Santos as president...or Jed Bartlet..but please not anyone Republican&#39;
  88. },
  89. {
  90. &#39;id&#39;: 4,
  91. &#39;requester&#39;: &#39;Barry Allen&#39;,
  92. &#39;description&#39;: &#39;A frictionless suit that wont burst into flames when I run at a zillion miles an hour...its for a friend&#39;
  93. },
  94. {
  95. &#39;id&#39;: 5,
  96. &#39;requeter&#39;: &#39;A. Bambaata&#39;,
  97. &#39;description&#39;: &#39;Boombox, prime condition, from the 80s. Go back in time if you have to&#39;
  98. }
  99. ];
  100. })();
  101. This is my index.html -
  102. &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js&quot;&gt;&lt;/script&gt;
  103. &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js&quot;&gt;&lt;/script&gt;
  104. &lt;script type=&quot;text/javascript&quot; src=&quot;/dashboard.js&quot;&gt;&lt;/script&gt;
  105. &lt;body&gt;
  106. &lt;div class=&quot;dashboard&quot;&gt;
  107. &lt;div ng-controller=&quot;JobsController as jobsCtrl&quot;&gt;
  108. &lt;div class=&quot;jobs&quot;&gt;
  109. {{jobsCtrl.jobs[0]}}
  110. &lt;/div&gt;
  111. &lt;/div&gt;
  112. &lt;/div&gt;
  113. &lt;/body&gt;
  114. This causes this error on my server
  115. 2015/07/29 12:18:02 http: panic serving [::1]:56857: runtime error: invalid memory address or nil pointer dereference
  116. goroutine 18 [running]:
  117. net/http.func&#183;009()
  118. /usr/local/go/src/pkg/net/http/server.go:1093 +0x9e
  119. runtime.panic(0x20ed40, 0x4efaed)
  120. /usr/local/go/src/pkg/runtime/panic.c:248 +0xda
  121. html/template.(*Template).escape(0x0, 0x0, 0x0)
  122. /usr/local/go/src/pkg/html/template/template.go:52 +0x30
  123. Why is this happening?
  124. </details>
  125. # 答案1
  126. **得分**: 0
  127. 你不能在Go模板中使用Angular的默认开始`{{`和结束`}}`括号,因为Go服务器会将你的Angular代码解释为[Go模板参数和管道](http://golang.org/pkg/text/template/)。
  128. 为了解决这个问题,你可以在配置中使用Angular的$interpolate来更改定义Angular代码的分隔符:
  129. ```javascript
  130. var app = angular.module('dashboard', []);
  131. app.config(function($interpolateProvider) {
  132. $interpolateProvider.startSymbol('[{');
  133. $interpolateProvider.endSymbol('}]');
  134. });

或者,如果你的页面是静态的,你可以直接使用Go内置的文件服务器,以避免与模板产生混淆:

http://golang.org/pkg/net/http/#example_FileServer

英文:

You cannot use angular's default start {{ and end }} brackets in a go template because the go server interprets your angular as go template arguments and pipelines.

> "Arguments" and "pipelines" are evaluations of data

To solve this you can make use of angular's $interpolate in your config to change the delimiters that define angular code:

  1. var app = angular.module(&#39;dashboard&#39;, []);
  2. app.config(function($interpolateProvider) {
  3. $interpolateProvider.startSymbol(&#39;[{&#39;);
  4. $interpolateProvider.endSymbol(&#39;}]&#39;);
  5. });

Alternatively if your pages are static you can just use Go's built in file server to not cause confusion with templates:

http://golang.org/pkg/net/http/#example_FileServer

huangapple
  • 本文由 发表于 2015年7月30日 03:32:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/31709533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定