Passing array via attribute to AngularJS directive

后端 未结 2 1687
忘掉有多难
忘掉有多难 2020-12-30 18:45

I\'m currently having a problem when passing an array to a directive via an attribute of that directive. I can read it as a String but i need it as an array so this is what

相关标签:
2条回答
  • 2020-12-30 19:00

    you can also have to use $scope instead of attrs. then you will get array object, otherwise you will get an string.

         scope:{
                title: "@",
                author: "@",
                content: "@",
                cover: "@",
                date: "@",
                tags: "="
            },
    
    
    link: function(scope, element, attrs){
                scope.tags = scope.tags
            }
    
    0 讨论(0)
  • 2020-12-30 19:06

    If you're accessing this array from your scope, i.e. loaded in a controller, you can just pass the name of the variable:

    Binding array to directive variable in AngularJS

    Directive:

    scope:{
            title: "@",
            author: "@",
            content: "@",
            cover: "@",
            date: "@",
            tags: "="
        },
    

    Template:

    <post title="sample title" tags="arrayName" ... >
    
    0 讨论(0)
提交回复
热议问题