source

Angularjs - 본문 요소 내부 지시어 가져오기

lovecheck 2023. 10. 4. 22:01
반응형

Angularjs - 본문 요소 내부 지시어 가져오기

얻는 방법body각도 지시의 요소? 내 목표는 jquery로 하는 것입니다.$('body').innerWidth();내부 지시jquery를 사용하는 것이 아니라 각진 내장 jqlite 구현을 사용하고 싶습니다.

다른 요소에 적용되는 지시 내에서 본문 요소에 액세스해야 하는 경우 $document 서비스를 사용할 수 있습니다.

app.directive("myDirective", function($document) {
  return function(scope, element, attr) {
    var bodyWidth = $document[0].body.clientWidth;
    console.log("width of body is "+ bodyWidth);
  };
});

<button my-directive>Sample Button</button>

jqLite에서 제공하는 DOM 순회 메서드를 사용할 수도 있습니다(jQuery가 제공하는 것보다 훨씬 덜 강력하지만).예를 들어, 를 사용하여 재귀적 조회를 수행할 수 있습니다.angular.element.parent()본문 태그를 찾는 방법.

다음을 사용하여 한 가지 변형을 더 합니다.find()angular의 jQLite에 포함된 구현:

app.directive("myDirective", function() {
    return function(scope, element, attr) {

    var body = angular.element(document).find('body');
    console.log(body[0].offsetWidth);

    };
});

언급URL : https://stackoverflow.com/questions/16725136/angularjs-get-body-element-inside-directive

반응형