如何评估包含{{}}的属性

时间:2013-06-14 12:22:43

标签: angularjs-directive

在以下指令中,我想评估{{selectedForApproval.length}}。这不是一个指令,但是一旦我把它放到一个指令中,我就不确定如何处理绑定。

HTML:<button-large color="green" ng-click="completeWork()" label="Approve Selected ({{selectedForApproval.length}})"></button-large>

指令:

directive('buttonLarge', function () {
    return {
        scope: false,
        replace: true,
        restrict: 'E',
        template: '<button type="checkbox" class="buttonL"/>',
        link: function (scope, element, attrs) {
            var config = {
                label: "Submit",
                color: "Default"
            };

            angular.extend(config, attrs);

            element.addClass("b"+capitalize(config.color));
            element.html(scope.$parent.$eval(config.label));

            //capitalize first letter of string
            function capitalize(s) {
                return s[0].toUpperCase() + s.slice(1);
            }
        }
    }
})

1 个答案:

答案 0 :(得分:1)

解决方案是使用转换...

<button-large color="green" ng-click="completeWork()">Approve Selected ({{selectedForApproval.length}})</button-large>

directive('buttonLarge', function () {
        return {
            scope: false,
            replace: true,
            restrict: 'E',
            transclude: true,
            template: '<button type="checkbox" class="buttonL" ng-transclude/>',
            link: function (scope, element, attrs) {
                var config = {
                    color: "Default"
                };

                angular.extend(config, attrs);

                element.addClass("b"+capitalize(config.color));

                //capitalize first letter of string
                function capitalize(s) {
                    return s[0].toUpperCase() + s.slice(1);
                }
            }
        }
    })