gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:52:15 27 4
gpt4 key购买 nike

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com