@ $ filter和$ filter之间的区别是什么?

时间:2014-06-11 07:42:01

标签: angularjs coffeescript x-editable

我是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. @j和$ j在角度上的差异
  2. 为什么我的情况不能按预期工作?
  3. 来自我的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>
    

    提前致谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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