IT Share you

AngularJS 지시문에 속성을 통해 배열 전달

shareyou 2021. 1. 8. 21:46
반응형

AngularJS 지시문에 속성을 통해 배열 전달


현재 해당 지시문의 속성을 통해 지시문에 배열을 전달할 때 문제가 있습니다. 나는 그것을 문자열로 읽을 수 있지만 배열로 필요하므로 이것이 내가 생각해 낸 것이지만 작동하지 않습니다. 누구 도와? 미리 thks

자바 스크립트 ::

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

스코프에서이 배열에 액세스하는 경우 (예 : 컨트롤러에로드 된 경우) 변수 이름 만 전달할 수 있습니다.

AngularJS의 지시어 변수에 배열 바인딩

지령:

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

주형:

<post title="sample title" tags="arrayName" ... >

attrs 대신 $ scope를 사용해야 할 수도 있습니다. 그런 다음 배열 객체를 얻습니다. 그렇지 않으면 문자열을 얻습니다.

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


link: function(scope, element, attrs){
            scope.tags = scope.tags
        }

참조 URL : https://stackoverflow.com/questions/16290782/passing-array-via-attribute-to-angularjs-directive

반응형