简体   繁体   English

Angular元素指令如何获取属性值

[英]Angular element directive how to get attribute value

I'm using an Angular element directive to generate an amount of stars (rating) for a given item. 我正在使用Angular元素指令为给定项目生成一定数量的星星(评级)。

I create a directive called 'generateStars' and I am using it like so in the view. 我创建了一个名为'generateStars'的指令,我在视图中使用它。

<generate-stars amount="3"></generate-stars>

I don't want the directive to be dependant on the current scope so I was wondering if I could take in the "amount" attribute and get the value within my directive function. 我不希望指令依赖于当前范围,所以我想知道我是否可以接受“amount”属性并获取我的指令函数中的值。

Here is that function: 这是这个功能:

angular.module('myapp.directives', [])

.directive('generateStars', function() {
    var html = '<i class="fa fa-star"></i>';

    return {
        restrict: 'E',
        template: html,
        replace: true
    };
});

I couldn't find any obvious documentation to allow me to get the value of 'amount' inside my directive function. 我找不到任何明显的文档来让我在指令函数中获得'amount'的值。

Any help would be much appreciated. 任何帮助将非常感激。 Thanks. 谢谢。

You could use isolated scope and use amount value can be passed as attribute as you are doing so. 您可以使用隔离范围,使用amount值可以作为属性传递。

Markup 标记

<generate-stars amount="{{amount}}"></generate-stars>

Controller 调节器

$scope.amount = 3; //passing it through the scope.

Directive 指示

angular.module('myapp.directives', [])

.directive('generateStars', function() {
    var html = '<i ng-repeat="i in [1, 2, 3, 4, 5]" class="fa fa-star" ng-if="i>=amount"></i>';

    return {
        restrict: 'E',
        template: html,
        replace: true,
        scope: { 
           amount: '@' //`@` for one way binding.
        }
    };
});

This can be without isolated scope. 这可以没有孤立的范围。 Basically you need to create class fa-3x in your directive template. 基本上,您需要在指令模板中创建类fa-3x

angular.module('myapp.directives', [])

.directive('generateStars', function() {
        return '<i ng-repeat="i in [1, 2, 3, 4, 5]" class="fa fa-star" ng-if="i>='+ attrs.amount +' "></i>'
    };

    return {
        restrict: 'E',
        template: html,
        replace: true
    };
});

You can use attrs object available in your link function of your Directive Definition Object. 您可以在指令定义对象的link功能中使用可用的attrs对象。

angular.module('myapp.directives', [])
.directive('generateStars', function() {
    var html = '<i class="fa fa-star"></i>';

    return {
        restrict: 'E',
        template: html,
        replace: true,
        link: function(scope, element, attrs) {
           /*all attributes passed to directive are available as properties
              on attrs object*/
            console.log(attrs.amount);
        }
    };
});

HTML HTML

<generate-stars amount="3"></generate-stars>

With that, when your directive is rendered, prints the value 3 in console. 这样,当渲染指令时,在控制台中输出值3。

I appreciate the help guys but neither of these solutions ended up working out for me. 我很感谢帮助人员,但这些解决方案都没有为我提供帮助。 This is what I ended up doing. 这就是我最终做的事情。

angular.module('myApp.directives', [])

.directive('generateStars', function() {
    var html = '<i class="fa fa-star" ng-repeat="n in [] | range:roundedAmount"></i>';

    return {
        restrict: 'E',
        scope: {
            amount: '='
        },
        link: function(scope, element, attrs) {
            scope.roundedAmount = parseInt(scope.amount);
        },
        replace: true,
        template: html
    };
})

.filter('range', function() {
    return function(input, total) {
        total = parseInt(total);
        for (var i=0; i<total; i++)
            input.push(i);

        return input;
    };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM