如何在coffeescript的角度非类控制器中使用'this':

时间:2014-11-03 18:02:12

标签: javascript angularjs coffeescript

我很好奇为什么以下不起作用,我认为这是我对coffeescript不了解的事情:



angular.module('clientApp')
  .controller 'FoobarCtrl', ($scope) ->

    console.log "Foobar controller"

    $scope.content = "message from foobar"

    @output = () ->
        return "returned message from coffee controller"




我认为以上内容相当于:



angular.module('clientApp')
  .controller('FoobazCtrl', function ($scope) {

    console.log( "Foobar controller" )

    $scope.content = "message from foobar";

    this.output = function(){ 
        return("returned message from coffee controller");
    }

  });




但它似乎并非如此。谁能告诉我哪里出了问题?

1 个答案:

答案 0 :(得分:1)

等价物是:

angular.module("clientApp").controller "FoobazCtrl", ($scope) ->
  console.log "Foobar controller"
  $scope.content = "message from foobar"
  @output = ->
    "returned message from coffee controller"

  return

您的coffescript代码正在生成此javascript:

angular.module('clientApp').controller('FoobarCtrl', function($scope) {
  console.log("Foobar controller");
  $scope.content = "message from foobar";
  return this.output = function() {
    return "returned message from coffee controller";
  };
});