javascript - 尝试访问指令中的数据时 AngularJs 崩溃

标签 javascript angularjs go

所以我是 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': 'I want you to give me the entire world'
        },
        {
            'id': 2,
            'requester': 'MJ Watson',
            'description': 'Please give me Spiderman, preferably upside down...but Im not fussy'
        },
        {   'id': 3,
            'requester': 'Josh Lyman',
            'description': 'Matthew Santos as president...or Jed Bartlet..but please not anyone Republican'
        }, 
        {
            'id': 4,
            'requester': 'Barry Allen',
            'description': 'A frictionless suit that wont burst into flames when I run at a zillion miles an hour...its for a friend'
        },
        {
            'id': 5,
            'requeter': 'A. Bambaata',
            'description': 'Boombox, prime condition, from the 80s. Go back in time if you have to'
        }
    ];

})();

这是我的 index.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

为什么会这样?

最佳答案

您不能在 go 模板中使用 angular 的默认开始 {{ 和结束 }} 括号,因为 go 服务器将您的 angular 解释为 go template arguments and pipelines .

"Arguments" and "pipelines" are evaluations of data

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

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

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

或者,如果您的页面是静态的,您可以只使用 Go 的内置文件服务器,以免与模板混淆:

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

关于javascript - 尝试访问指令中的数据时 AngularJs 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31709533/

相关文章:

Javascript Trivia 游戏 - 随机时有重复的问题

javascript - AngularJS - 如何让 Controller 以简单的方式注册窗口全局变量的每次更改?

javascript - 如何将 $scope 传递给 angular.js 中的服务?

Node.js 应用程序适用于工头,但不适用于 heroku。 404错误

go - 我可以在 cgo 中使用 libtool 输出吗?

parsing - time.Parse 自定义布局

json - 如何使用脚本更改 Deployment 的 ConfigMap?

javascript - 如何在没有 enablePrivilege 的情况下将 View 对象分配给 XUL 树小部件

javascript - 如何检测用户使用浏览器中的“后退”按钮打开页面?

javascript - 我可以在浏览器中隐藏特定字符 (,) 吗?