从Coffeescript中的指令调用服务中的方法时出错

时间:2017-01-17 22:05:38

标签: javascript angularjs service coffeescript directive

我有一个名为DashboardLayoutStyleCalculatorService的服务,其中包含以下方法。删除了其他方法,因为它们与问题无关:

  styleFns =
    table: @computeStyleForTable
    single_value: @computeStyleForSingleValue

  @computeStyleFor = (type, height = null) ->
    return styleFns[type](height)

通过两个不同的指令调用此服务:

指令1:

scope.computeStyle = (component) ->
  if component?.height?
    height = component.height * 50
  return DashboardLayoutStyleCalculator.computeStyleFor(component.element.type, height)

指令2:

scope.computeStyle = (element, rowComponent) ->
  return DashboardLayoutStyleCalculator.computeStyleFor(element.type, rowComponent?.height?)

我的代码在一个坚果shell中做的是,根据computeStyleFor中的输入类型,它为表或单个值可视化调用两种不同的方法。还有一些动态高度计算基于某些参数,这些参数对于这个问题并不是完全必要的。

当我在浏览器中运行此代码时,出现以下错误:

main.webpack-a9f3967e.js:27 TypeError: e[t] is not a function
    at Object.computeStyleFor (https://localhost:54321/webpack/main.webpack-a9f3967e.js:10:17938)
    at e.t.computeStyle (https://localhost:54321/webpack/main.webpack-a9f3967e.js:10:16879)
    at fn (eval at compile (https://localhost:54321/webpack/vendor.webpack-6f544239.js:49:29700), <anonymous>:4:627)
    at d.$digest (https://localhost:54321/webpack/vendor.webpack-6f544239.js:48:11027)
    at d.$apply (https://localhost:54321/webpack/vendor.webpack-6f544239.js:48:12692)
    at https://localhost:54321/webpack/vendor.webpack-6f544239.js:47:23881 undefined

computeStyleForstyleFns似乎存在问题。

1 个答案:

答案 0 :(得分:0)

从我看到的情况来看,我认为你的电话def funcWithParam(param, *args): print "Your parameter is: " + param def justFunction(*args): print "No parameters" def wrong(*args): print "wrong choice" userInput = raw_input("type 'params' for parameters. type 'no' for no parameters: ") if userInput == "params": myparam = raw_input("type your parameter: ") else: myparam = "" dic = { "params": funcWithParam, "no": justFunction, } dic.get(userInput, wrong)(myparam) 会产生一个未经检验的密钥

component.element.type

因此您稍后致电

scope.computeStyle = (component) ->
    if component?.height?
        height = component.height * 50
    return DashboardLayoutStyleCalculator.computeStyleFor(component.element.type, height)
                                                          ^^^^^^^^^^^^^^^^^^^^^^
                                                                |
                                                                ---- Not what you expect

添加一些输出以查看表达式 @computeStyleFor = (type, height = null) -> return styleFns[type](height) ^^^^^^ | --- fails as styleFns[type] yields undefined which is not a function type会产生什么