【问题标题】:What's the difference between @$filter and $filter?@$filter 和 $filter 有什么区别?
【发布时间】:2014-06-12 02:40:40
【问题描述】:

我是 angularjs 的新手。我正在使用angular-xeditable。我的视图中有可选字段。选择字段的默认值有问题(最后选择)。

这工作没有错误,但不正确

class MyCtrl
  constructor: ($scope, @$filter, @$log) ->
    actions=[]


  showActions: (action) ->
    selected = []
    if action then selected = $filter('filter')(@actions, {name: action})
    if selected.length then return selected[0].name else return "Not set"

controllersModule.controller 'MyCtrl', MyCtrl

如果我使用$filter 而不使用@,则会出现错误:

class MyCtrl
  constructor: ($scope, $filter, @$log) ->
    actions=[]


  showActions: (action) ->
    selected = []
    if action then selected = $filter('filter')(@actions, {name: action})
    if selected.length then return selected[0].name else return "Not set"

controllersModule.controller 'MyCtrl', MyCtrl

错误:[$interpolate:interr] 无法插值: {{ mc.showActions(flow.action)}}

ReferenceError: $filter 未定义

我的问题是:

  1. angularjs 中 @$ 和 $ 有什么区别
  2. 为什么在我的情况下没有按预期工作?

我的 html 中的一段代码

   <div ng-controller="MyCtrl as mc">
    ...
    <td>
        <span editable-select="flow.action" e-name="action" onshow="mc.getActions()"
              e-ng-options="action.id as action.name for action in mc.actions"
              e-form="rowform" e-required>
            {{ mc.showActions(flow.action)}}
        </span>
    </td>
    ...
</div>

提前致谢。

【问题讨论】:

  • 现在,仅在代码示例中的选项卡就会导致问题。 showActions 将位于一个单独的对象上,而不是 MyCtrl 的一部分。

标签: angularjs coffeescript x-editable


【解决方案1】:

这里的问题与 `editable-select` 和 `e-ng-options` 相关。问题是,它们的值并不相等。我是说

editable-select="flow.action"
e-ng-options="action.id as action.name for action in mc.actions"

flow.action = "string"
action.id = 5

5 not equal to "string"

事实上,没有filter 也可以。

【讨论】:

    【解决方案2】:

    您不能只使用$filter 的原因是因为您没有来自showActions 实例方法的该参数的上下文。在构造函数中使用@$filter 与将其分配给构造函数主体中的this.$filter 相同;创建该函数/对象的本地副本以在实例对象的上下文中使用。如果不将其分配给MyCtl 的实例可以访问的本地副本,则它在构造函数之外不再可用。

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 2011-10-17
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 2014-02-05
      相关资源
      最近更新 更多