指令中指定的过滤器值给一个变量 [英] Assign the filter value to a variable within a directive
问题描述
我能做到这一点(职称)在我的控制器和我在想,如果我能做到这一点对我的指令了。
I am able to do it(title) on my controllers and I was wondering if I can do it on my directives too.
比方说,我注入过滤器,以我的指令,像这样...
Lets say that I inject the filter to my directive like this...
app.directive('ngDirective',
['$compile','$filter',
function ($compile, $filter) {
'use strict';
和链接功能里面我有以下。
And inside the link function I have the following.
var filter = $filter('i18n');
var requiredMessage = filter('is_required');
我知道,在指令我失去了我$范围,并且可能是原因。
I know that on the directive I lose my $scope and that might be the reason.
编辑:这是我的code HTTP的preVIEW的plunker:// plnkr。 CO /编辑/ u1L9SHdAvZnvEqwVBeJg
Here is the plunker with the preview of my code http://plnkr.co/edit/u1L9SHdAvZnvEqwVBeJg
如何解决这个问题有什么想法?
Any thoughts on how to fix this issue?
推荐答案
如果您要使用特定的过滤器,你必须加载它。
If you want to use certain filter, you'll have to load it.
下面是定义滤波器和负载到指令模块的示例
Here is the example of defining filter and load into your directive module.
http://plnkr.co/edit/61X6i32By8PA6W7BLh52?p=$p$ PVIEW
angular.module('myfilters', []).
filter('i18n', function() {
return function() { return 'my message'; };
});
angular.module('myapp', ['myfilters']).
directive('ngTest', function($compile, $filter) {
'use strict';
这篇关于指令中指定的过滤器值给一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!