通过属性将数组传递给 AngularJS 指令 [英] Passing array via attribute to AngularJS directive
本文介绍了通过属性将数组传递给 AngularJS 指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前在通过该指令的属性将数组传递给指令时遇到问题.我可以将其读取为字符串,但我需要将其作为数组,所以这是我想出的,但它不起作用.帮助任何人?提前考虑
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::
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:
指令:
scope:{
title: "@",
author: "@",
content: "@",
cover: "@",
date: "@",
tags: "="
},
模板:
<post title="sample title" tags="arrayName" ... >
这篇关于通过属性将数组传递给 AngularJS 指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文