一尘不染

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

angularjs

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

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

阅读 235

收藏
2020-07-04

共1个答案

一尘不染

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

指示:

scope:{
        title: "@",
        author: "@",
        content: "@",
        cover: "@",
        date: "@",
        tags: "="
    },

模板:

<post title="sample title" tags="arrayName" ... >
2020-07-04