Arrays 通过属性将数组传递给AngularJS指令
当前,当通过指令的属性将数组传递给该指令时,我遇到了一个问题。我可以把它读成一个字符串,但我需要它作为一个数组,所以这是我想出来的,但它不起作用。有人帮忙吗?提前付款 Javascript::Arrays 通过属性将数组传递给AngularJS指令,arrays,parsing,angularjs,attributes,directive,Arrays,Parsing,Angularjs,Attributes,Directive,当前,当通过指令的属性将数组传递给该指令时,我遇到了一个问题。我可以把它读成一个字符串,但我需要它作为一个数组,所以这是我想出来的,但它不起作用。有人帮忙吗?提前付款 Javascript:: app.directive('post', function($parse){ return { restrict: "E", scope:{ title: "@", author: "@",
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']" ... >
如果从作用域访问此数组,即加载到控制器中,则只需传递变量名称: 指令:
scope:{
title: "@",
author: "@",
content: "@",
cover: "@",
date: "@",
tags: "="
},
<post title="sample title" tags="arrayName" ... >
模板:
scope:{
title: "@",
author: "@",
content: "@",
cover: "@",
date: "@",
tags: "="
},
<post title="sample title" tags="arrayName" ... >
您也可以使用$scope而不是attrs。然后您将获得数组对象,否则将获得字符串
scope:{
title: "@",
author: "@",
content: "@",
cover: "@",
date: "@",
tags: "="
},
link: function(scope, element, attrs){
scope.tags = scope.tags
}
如果它是一个内联数组:
tags=“[1,2,3]”
?编辑:我在这里找到了答案:当我执行console.log(scope.tags)
时,标签是未定义的。怎么了