簡體   English   中英

如何在角度指令的鏈接函數中傳遞值

[英]How to pass value in link function of a angular directive

親愛的我有指示

function percentview($compile) {
    var linker = function (scope, element) {

        var controldef = "";
        switch (scope.percentage.state) {
            case 'stable':
                controldef = ' <span class="text-yellow font-17">' +
                    '  <i>0%</i>' +
                    '<i class="fa "></i> </span>';



                break;
            case 'up':
                controldef = ' <span class="text-issue font-17">' +
                   '  <i>+ {{percentage.percentage}}%</i>' +
                   '<i class="fa fa-arrow-up"></i> </span>';
                break;
            case 'down':
                controldef = ' <span class="text-green font-17">' +
                  '  <i- {{percentage.percentage}}%</i>' +
                  '<i class="fa fa-arrow-down"></i> </span>';
                break;
            default:
                break;
        }
        element.html($compile(controldef));
    };
    var directive = {
        restrict: 'E',

        scope: {
            percentage: '='
        },
        link: linker

    }

    return directive;
}

percentview.$inject = ['$compile'];
angular
 .module('app')
 .directive('percentview', percentview);

html://

<percentview percentage="percentiles[0]"></percentview>

但是,當我運行代碼時,我收到錯誤scope.percentage的定義,我在鏈接函數中記錄了作用域對象,並且可以看到具有所有值的稱為百分比的屬性(對象)。

我如何在鏈接函數中傳遞對象。 或此代碼有什么問題。

在我看來,將變量傳遞給鏈接函數似乎不錯。

如果您傳遞文字,如下面的jbin所示,它可以正常工作,也許您遇到了范圍問題,並且percentiles [0]在該范圍上不可用

http://jsbin.com/zodeva/edit?html,js,控制台

您可以嘗試element.percentage.state嗎? 如我所見,百分比是元素的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM