dongshun7312 2015-07-29 19:32
浏览 55
已采纳

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

So I'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('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'
        }
    ];

})();

This is my 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>

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·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?

  • 写回答

1条回答 默认 最新

  • dongshi1880 2015-07-29 19:38
    关注

    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('dashboard', []);
    
    app.config(function($interpolateProvider) {
        $interpolateProvider.startSymbol('[{');
        $interpolateProvider.endSymbol('}]');
    });
    

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)