arrays - 通过属性将数组传递给 AngularJS 指令

标签 arrays parsing angularjs attributes directive

目前,我在通过指令的属性将数组传递给指令时遇到问题。我可以将它作为字符串读取,但我需要它作为数组,所以这就是我想出的,但它不起作用。帮助任何人吗?提前致谢

Javascript::

app.directive('post', function($parse){
    return {
        restrict: "E",
        scope:{
            title: "@",
            author: "@",
            content: "@",
            cover: "@",
            date: "@"
        },
        templateUrl: 'components/postComponent.html',
        link: function(scope, element, attrs){
            scope.tags = $parse(attrs.tags)
        }
    }
}

HTML::

<post title="sample title" tags="['HTML5', 'AngularJS', 'Javascript']" ... >

最佳答案

如果您从作用域访问此数组,即加载到 Controller 中,则只需传递变量的名称即可:

Binding array to directive variable in AngularJS

指令:

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

模板:

<post title="sample title" tags="arrayName" ... >

关于arrays - 通过属性将数组传递给 AngularJS 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16290782/

相关文章:

javascript - 当过滤的搜索栏不返回任何内容时,如何 ngShow a div?

c++ - 数组与 vector : Introductory Similarities and Differences

php - 如何从 php 数组创建一个 javascript 数组?

c# - 最快的多线程解析串口数据的方法C#

html - 使用angular js在html中显示json嵌套对象的数据

javascript - 如何在应用程序中注入(inject)/加载 Angular 指令

arrays - Delphi 10.2.3中的Setlength用于类似数组的类型

c - 数组中的大多数元素分而治之 O(N.log(N))

python - 在 python 中提取表单键值对表单 html 页面或解析 html 页面

Python - NLP - 将 iter(iter(tree)) 转换为 list(tree)