Angularjs与Coffeescript类

kun*_*jee 6 coffeescript angularjs

我正在尝试使用Coffescript课程的angularjs

我能够注射并能够使用coffeescript做一个成功的例子.但要访问$ scope,我必须在构造函数中编写函数.我能做些什么来摆脱它.如果有另一种好的方式来写,请告诉我.

这是我的工作coffeescript代码

class PersonCtrl
    @$inject = ['$scope']

    constructor: (@scope) ->
        @scope.persons = [
            firstName:"Kunjan"
            lastName:"Dalal"
        ,
            firstName:"Kunj"
            lastName:"Dalal"
        ]

        @scope.addPerson = () =>
            @scope.persons.push angular.copy @scope.person 
Run Code Online (Sandbox Code Playgroud)

如果需要进一步的详细信息,请告诉我.

mal*_*lix 11

我使用了以下语法:

app = angular.module 'myapp', []

class MySimpleCtrl

  @$inject: ['$scope'] 
  constructor: (@scope) ->
    @scope.demo = 'demo value'
    @scope.clearText = @clearText

  clearText: =>
    @scope.demo = ""

app.controller 'MySimpleCtrl', MySimpleCtrl

angular.bootstrap document, ['myapp']
Run Code Online (Sandbox Code Playgroud)

看看这个jsFiddle:http: //jsfiddle.net/jwcMA/

UPDATE @ oto-brglez .bootstrap()调用替换了你的<html>标签上的ng-app

更新 @TylerCollier,这是一段时间了,现在我可能会使用Controller As表示法(或者TypeScript!)

咖啡

class PetController 
    constructor: (@$scope) ->
        @pets = ['Fido','Felix']
    addPet: (pet) ->
        @pets.push(pet)
Run Code Online (Sandbox Code Playgroud)

HTML

<div ng-controller="PetController as pc">
...
<li ng-repeat="pet in pc.pets">
Run Code Online (Sandbox Code Playgroud)