通过属性传递数组AngularJS指令 [英] Passing array via attribute to AngularJS directive

查看:1013
本文介绍了通过属性传递数组AngularJS指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过该指令的属性将数组传递给一个指令时,目前有一个问题。我可以读它作为一个字符串,但我需要它作为一个数组所以这就是我想出了,但它不工作。帮助人?提前thks

的Javascript:

  app.directive('后',函数($解析){
    返回{
        限制:E,
        范围:{
            标题:@,
            作者:@,
            内容:@,
            包括:@,
            日期:@
        },
        templateUrl:'组件/ postComponent.html',
        链接:功能(范围,元素,ATTRS){
            scope.tags = $解析(attrs.tags)
        }
    }
}

HTML ::

 <文章标题=样本标题标签=[HTML5,AngularJS','的Javascript']...>


解决方案

如果您在访问您的适用范围,即在控制器中加载这个数组,你可以通过该变量的名称:

<一个href=\"http://stackoverflow.com/questions/14695792/binding-array-to-directive-variable-in-angularjs\">Binding数组变量的指令在AngularJS

指令:

 范围:{
        标题:@,
        作者:@,
        内容:@,
        包括:@,
        日期:@,
        标签:=
    },

模板:

 &LT;文章标题=样本标题标签=arrayName中的...&GT;

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 i came up with but it doesn't work. Help anyone? thks in advance

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']" ... >

解决方案

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" ... >

这篇关于通过属性传递数组AngularJS指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆