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