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

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

AngularJs crashes when trying to access data in directive

问题

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

(function() {
	var app = angular.module('dashboard', []);
	app.controller("JobsController", function() {
		this.currentJob = 0;
		this.jobs = jobs;
		this.setCurrentJob = function(index) {
			this.currentJob = index; 
		};		
	});

	var jobs = [
		{
			'id': 1,
			'requester': 'Prakash Sanker',
			'description': '我想让你给我整个世界'
		},
		{
			'id': 2,
			'requester': 'MJ Watson',
			'description': '请给我蜘蛛侠,最好是倒挂着...但我不挑剔'
		},
		{
			'id': 3,
			'requester': 'Josh Lyman',
			'description': 'Matthew Santos当总统...或者Jed Bartlet...但请不要是共和党人'
		}, 
		{
			'id': 4,
			'requester': 'Barry Allen',
			'description': '一套无摩擦的服装,当我以每小时数千英里的速度奔跑时不会起火...这是给一个朋友的'
		},
		{
			'id': 5,
			'requeter': 'A. Bambaata',
			'description': '80年代的音乐播放器,状态良好。如果必要,可以倒回到过去'
		}
	];

})();

这是我的index.html

```html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script type="text/javascript" src="/dashboard.js"></script>
<body>
	<div class="dashboard">
		<div ng-controller="JobsController as jobsCtrl">
			<div class="jobs">
				{{jobsCtrl.jobs[0]}}
			</div>
		</div>
	</div>
</body>

这导致我的服务器出现以下错误:

2015/07/29 12:18:02 http: panic serving [::1]:56857: runtime error: invalid memory address or nil pointer dereference
goroutine 18 [running]:
net/http.func·009()
	/usr/local/go/src/pkg/net/http/server.go:1093 +0x9e
runtime.panic(0x20ed40, 0x4efaed)
	/usr/local/go/src/pkg/runtime/panic.c:248 +0xda
html/template.(*Template).escape(0x0, 0x0, 0x0)
	/usr/local/go/src/pkg/html/template/template.go:52 +0x30

为什么会发生这种情况?

<details>
<summary>英文:</summary>

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-

    (function() {
    	var app = angular.module(&#39;dashboard&#39;, []);
    	app.controller(&quot;JobsController&quot;, function() {
    		this.currentJob = 0;
    		this.jobs = jobs;
    		this.setCurrentJob = function(index) {
    			this.currentJob = index; 
    		};		
    	});
    
    	var jobs = [
    		{
    			&#39;id&#39;: 1,
    			&#39;requester&#39;: &#39;Prakash Sanker&#39;,
    			&#39;description&#39;: &#39;I want you to give me the entire world&#39;
    		},
    		{
    			&#39;id&#39;: 2,
    			&#39;requester&#39;: &#39;MJ Watson&#39;,
    			&#39;description&#39;: &#39;Please give me Spiderman, preferably upside down...but Im not fussy&#39;
    		},
    		{	&#39;id&#39;: 3,
    			&#39;requester&#39;: &#39;Josh Lyman&#39;,
    			&#39;description&#39;: &#39;Matthew Santos as president...or Jed Bartlet..but please not anyone Republican&#39;
    		}, 
    		{
    			&#39;id&#39;: 4,
    			&#39;requester&#39;: &#39;Barry Allen&#39;,
    			&#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;
    		},
    		{
    			&#39;id&#39;: 5,
    			&#39;requeter&#39;: &#39;A. Bambaata&#39;,
    			&#39;description&#39;: &#39;Boombox, prime condition, from the 80s. Go back in time if you have to&#39;
    		}
    	];
    
    })();

This is my index.html - 

    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;/dashboard.js&quot;&gt;&lt;/script&gt;
    &lt;body&gt;
    	&lt;div class=&quot;dashboard&quot;&gt;
    		&lt;div ng-controller=&quot;JobsController as jobsCtrl&quot;&gt;
    			&lt;div class=&quot;jobs&quot;&gt;
    				{{jobsCtrl.jobs[0]}}
    			&lt;/div&gt;
    		&lt;/div&gt;
    	&lt;/div&gt;
    &lt;/body&gt;

This causes this error on my server 

    2015/07/29 12:18:02 http: panic serving [::1]:56857: runtime error: invalid memory address or nil pointer dereference
    goroutine 18 [running]:
    net/http.func&#183;009()
    	/usr/local/go/src/pkg/net/http/server.go:1093 +0x9e
    runtime.panic(0x20ed40, 0x4efaed)
    	/usr/local/go/src/pkg/runtime/panic.c:248 +0xda
    html/template.(*Template).escape(0x0, 0x0, 0x0)
    	/usr/local/go/src/pkg/html/template/template.go:52 +0x30

Why is this happening? 

</details>


# 答案1
**得分**: 0

你不能在Go模板中使用Angular的默认开始`{{`和结束`}}`括号,因为Go服务器会将你的Angular代码解释为[Go模板参数和管道](http://golang.org/pkg/text/template/)。

为了解决这个问题,你可以在配置中使用Angular的$interpolate来更改定义Angular代码的分隔符:

```javascript
var app = angular.module('dashboard', []);

app.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('[{');
    $interpolateProvider.endSymbol('}]');
});

或者,如果你的页面是静态的,你可以直接使用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:

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

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:

确定