当我通过一个指令的属性传递一个数组给那个指令时,我现在遇到了一个问题。我可以把它读成一个字符串,但我需要它作为一个数组,所以这就是我想出来的,但它不起作用。对谁有帮助吗?提前思考
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']" ... >
发布于 2016-05-29 03:50:52
你也可以使用$scope而不是attrs。然后你将得到数组对象,否则你将得到一个字符串。
scope:{
title: "@",
author: "@",
content: "@",
cover: "@",
date: "@",
tags: "="
},
link: function(scope, element, attrs){
scope.tags = scope.tags
}
https://stackoverflow.com/questions/16290782
复制相似问题