在AngularJS控制器中使用CoffeeScript类

时间:2015-08-05 10:50:35

标签: javascript angularjs class coffeescript

我有一个用CoffeeScript编写的演示者类。它位于AngularJS控制器之前加载的文件中。但我不能在那里上课。我怎么能用它?

主持人:

class CompanyPresenter
  constructor: (company) ->
    @company = company

  location: ->
    if @company.business.city && @company.country_id
      @company.business.city + ', ' + @company.country_id
    else if @company.business.city
      @company.business.city
    else if @company.country_id
      @company.country_id
    else
      undefined

控制器:

@Sim.controller 'CompanyShowCtrl', ['$scope', '$http', '$stateParams',
   'language'
  ($scope, $http, $stateParams, language) ->

    $http.get('businesses/' + $stateParams.id).success((businessAttributes) ->
      $scope.business = businessAttributes
      # this doesn't work :(
      $scope.businessPresenter = new CompanyPresenter($scope.business)
    )

我尽量避免使用servicefactory,因为我不需要单身,我想用我的模型初始化它并能够重复使用它。

1 个答案:

答案 0 :(得分:0)

我在课程定义后添加了这一行

@Sim.value('CompanyPresenter', CompanyPresenter)

并将'CompanyPresenter'注入CompanyShowCtrl