Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 通过属性将数组传递给AngularJS指令_Arrays_Parsing_Angularjs_Attributes_Directive - Fatal编程技术网

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

Arrays 通过属性将数组传递给AngularJS指令,arrays,parsing,angularjs,attributes,directive,Arrays,Parsing,Angularjs,Attributes,Directive,当前,当通过指令的属性将数组传递给该指令时,我遇到了一个问题。我可以把它读成一个字符串,但我需要它作为一个数组,所以这是我想出来的,但它不起作用。有人帮忙吗?提前付款 Javascript:: app.directive('post', function($parse){ return { restrict: "E", scope:{ title: "@", author: "@",

当前,当通过指令的属性将数组传递给该指令时,我遇到了一个问题。我可以把它读成一个字符串,但我需要它作为一个数组,所以这是我想出来的,但它不起作用。有人帮忙吗?提前付款

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']" ... >

如果从作用域访问此数组,即加载到控制器中,则只需传递变量名称:

指令:

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)
时,标签是未定义的。怎么了