AngularJS - 从自定义过滤器中的控制器访问 $scope [英] AngularJS - Access $scope from controller in custom filter

查看:25
本文介绍了AngularJS - 从自定义过滤器中的控制器访问 $scope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有各种 $scopescontroller.我需要在自定义过滤器中访问这些 $scopes 之一:

I have a controller with various $scopes. I need to access one of these $scopes in a custom filter:

app.controller('AppController',
    function($scope) {
      $scope.var1 = 'Some data1';
      $scope.var2 = 'Some data2';
    }
  );

app.filter('myCustomFilter', function ($filter) {

  return function (date) {
    var date = date;
  };
});


<tr ng-repeat="data in MyData" ng-class="{true: 'green', false:'red'}[data.Date | myCustomFilter]">

</tr>

如何将 $scope.var1 传递到我的 myCustomFilter 中??

How can i pass $scope.var1 into my myCustomFilter??

推荐答案

您必须向过滤器提供所需的范围属性.

You must provide the wanted scope attribute to the filter.

你可以这样做:

过滤器:

app.filter('myCustomFilter', function ($filter) {

  return function (date,myVar1) {
/* Do some stuff with myVar1 */
  };
});

HTML:

<tr ng-repeat="data in MyData" ng-class="{true: 'green', false:'red'}[data.Date | myCustomFilter:var1]">

</tr>

这篇关于AngularJS - 从自定义过滤器中的控制器访问 $scope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆