【问题标题】:Use CoffeeScript class in AngularJS controller在 AngularJS 控制器中使用 CoffeeScript 类
【发布时间】:2015-08-06 01:11:09
【问题描述】:

我有一个用 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,因为我不需要单例,我想用我的模型初始化它并能够重用它。

【问题讨论】:

  • 将解决方案发布为答案而不是问题。

标签: javascript angularjs class coffeescript


【解决方案1】:

我在类定义之后添加了这一行

@Sim.value('CompanyPresenter', CompanyPresenter)

并将'CompanyPresenter' 注入CompanyShowCtrl

【讨论】:

    猜你喜欢
    • 2014-12-24
    • 2013-11-01
    • 1970-01-01
    • 2015-05-12
    • 2015-05-27
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多