2016-06-10 53 views
0

我正在努力学习Angular 2的开发,所以请原谅基本问题。我使用了Angular-full-stack(https://github.com/angular-fullstack/generator-angular-fullstack)来生成一个骨架应用程序。除此之外,它预先生成客户端/应用程序/主文件中的5个文件:main.controller.js,main.controller.spec.js,main.html,main.js,main.scss角度2:我在哪里指定其他方法?

main.controller .js文件

'use strict'; 

(function() { 

    class MainController { 

    constructor($http) { 
     this.$http = $http; 
     this.awesomeThings = []; 
    } 

    $onInit() { 
     this.$http.get('/api/things') 
     .then(response => { 
      this.awesomeThings = response.data; 
     }); 
    } 

    addThing() { 
     if (this.newThing) { 
     this.$http.post('/api/things', { 
      name: this.newThing 
     }); 
     this.newThing = ''; 
     } 
    } 

    deleteThing(thing) { 
     this.$http.delete('/api/things/' + thing._id); 
    } 
    } 

    angular.module('orbitApp') 
    .component('main', { 
     templateUrl: 'app/main/main.html', 
     controller: MainController 
    }); 
})(); 

有一个构造函数和一些方法已经预先定义。但是,试图访问这些或任何其他我从main.html中添加不起作用:

main.html中

... 
<button onclick="deleteThing()">Test: deletething</button> 
... 

单击该按钮提供了以下控制台错误: “未捕获的ReferenceError :deleteThing未定义“

如何或在哪里添加他/她自己的方法,然后以他们可以访问的方式?对于这个问题,什么应该进入main.js?

回答

2
<button (click)="deleteThing()">Test: deletething</button> 

参考: https://angular.io/guide/cheatsheet

+0

这让错误消失,但点击按钮不会做任何事情。猜猜还有一些其他的东西。 –