如何确定是否一个可选功能已经传递到一个角指令? [英] How to determine if an optional function has been passed into an Angular directive?

查看:106
本文介绍了如何确定是否一个可选功能已经传递到一个角指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示一些数据的指令,但我想的指令用户能够控制数据的显示方式。我想允许用户能够控制使用三个选项之一显示。

I have a directive that displays some data but I want to user of the directive to be able to control how that data is displayed. I would like to allow a user to be able control the display using one of three options.


  1. 通在字符串中使用的显示器

  2. 传中的函数将被调用来生成字符串

  3. 或只显示原始数据

我不能发布整个指令中的code(NDA和所有),但它是显示使用D3环图指令。在中间的数目是一块有问题的数据。因此,假如环图表表示百分比,我可能要在中央的文字说55%。因此,假如myvalue的是范围的属性,并设置为55,这里是我想做些什么:

I can't post the code for the entire directive (NDA and all) but it is a directive for showing a ring chart using D3. The number in the middle is the piece of data in question. So assuming the ring chart is showing a percentage, I may want the text in the center to say 55%. So assuming myValue is a property on the scope and is set to 55, here is what I would like to do:

<div ring-chart total="100"
             remaining="{{myValue}}"
             center-number-class="center-number-sm"
             remaining-color="#0F79C0"
             used-color="#C1D3E6"
             chart-width="45"
             chart-height="45"
             ring-thickness="3"
             label-text="{{myValue}}%"></div>

这将显示出55%的
或做:

which would show 55% or do:

<div ring-chart total="100"
             remaining="{{myValue}}"
             center-number-class="center-number-sm"
             remaining-color="#0F79C0"
             used-color="#C1D3E6"
             chart-width="45"
             chart-height="45"
             ring-thickness="3"
             label-function="ringLabelFunction(remaining)"></div>

这将显示出任何ringLabelFunction(value)返回
最后需要做的选项:

which would show whatever ringLabelFunction(value) returns and finally have the option to do:

<div ring-chart total="100"
             remaining="{{myValue}}"
             center-number-class="center-number-sm"
             remaining-color="#0F79C0"
             used-color="#C1D3E6"
             chart-width="45"
             chart-height="45"
             ring-thickness="3"></div>

这将只显示55。

which would just show 55.

在该指令我有

    ...
scope: {
    total:"@",
    remaining:"@",
    valueSuffix: "@",
    centerNumberClass: "@",
    suffixClass: "@",
    remainingColor: "@",
    totalColor: "@",
    chartWidth: "@",
    chartHeight: "@",
    ringThickness: "@",
    labelFunction: "&",
    labelText:"@"
},
link:function($scope, element, attrs) {
    var labelContent;
    if ($scope.labelText) {
     labelContent =  $scope.labelText;
    } else if ($scope.labelFunction) { //<-- this is always true
     labelContent = $scope.labelFunction({remaining:$scope.remaining});
    } else { //so I never get to this
     labelContent = $scope.remaining;
    }

   ...
}

...

所以,总之我正在寻找一种方法来确定是否$ scope.labelFunction实际上已设置。

So, in short I am looking for a way to determine if $scope.labelFunction has actually been set.

推荐答案

您必须链接 ATTRS 。只是检查标签功能的定义,它的值是指函数

You have link attrs. Just check if label-function is defined and its value refers to function

link:function($scope, element, attrs) {

   if(attrs.labelFunction != undefined && typeof(attrs.onStuff) == 'function'){
        scope.$eval(attrs.labelFunction);
    }
}

这篇关于如何确定是否一个可选功能已经传递到一个角指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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