【发布时间】: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)
)
我尽量避免使用service 或factory,因为我不需要单例,我想用我的模型初始化它并能够重用它。
【问题讨论】:
-
将解决方案发布为答案而不是问题。
标签: javascript angularjs class coffeescript