programing

각도 지시어에 변수 전달

lastcode 2023. 3. 23. 22:47
반응형

각도 지시어에 변수 전달

제가 지시가 있다면myDir나는 그것을 '내부'라고 부른다.ng-repeat그렇게

<my-dir myindex="{{$index}}"></my-dir>

접속 방법myindex실제 스트링을 얻을 수 있습니다.{{$index}}사용할 때attrs.myindex이내에postLink기능.html을 검사하면 실제로 다음과 같이 표시됩니다.myindex="2".

해라

<my-dir myindex="$index"></my-dir>

그리고나서

app.directive('myDir', function () {
  return {
    restrict: 'E',
    scope: {
      myindex: '='
    },
    template:'<div>{{myindex}}</div>',
    link: function(scope, element, attrs){
      scope.myindex = attrs.myindex;
      console.log('test', scope.myindex)
    }
  };
})

데모: 플런커

언급URL : https://stackoverflow.com/questions/16432198/passing-variable-to-angular-directive

반응형