source

html 태그에 ng-app과 ng-controller를 선언하는 것이 나쁜가요?

itover 2023. 3. 8. 21:05
반응형

html 태그에 ng-app과 ng-controller를 선언하는 것이 나쁜가요?

둘 다 신고하는 게 나쁜 관행인가요?ng-app그리고.ng-controller에서<html>태그?

예를 들어 다음과 같습니다.<html class="no-js" ng-app="myApp" ng-controller="MainCtrl">

이것이 나쁜 관행으로 간주됩니까?저는 이 시스템을 제어하려고 합니다.<title>어플리케이션의 태그를 동적으로 표시하기 때문에MainCtrl컨트롤러의 범위는 어플리케이션의 나머지 부분에서 중요하기 때문에 컨트롤러의 범위를 조기에 파악할 수 있습니다.

그럼 내가 쓸 수 있어<title>{{settings.title}}</title>에서MainCtrl컨트롤러 및 하위 컨트롤러가 액세스하도록 합니다.$scope.$parent.settings.title = "hello world";

에 접속하여 제목을 설정할 수 있어야 합니다.$window추상화, 그 결과 컨트롤러가 필요 없게 됩니다.html태그를 붙입니다.

아직까지는 문제가 없을 것 같아요.유일한 영향은 루트 스코프가 html 마크업에서 읽을 수 없게 되는 것입니다.이는 컨트롤러 스코프가 루트 스코프를 덮어쓰기 때문입니다.

예를들면,

<div id="parent" ng-app>
  <div id="child" ng-controller='...'>

$rootScope.$id = 002    // rootscope from $rootscope service
$("#parent").scope().$id =002 // rootscope scope get from element scope    
$("#child").scope().$id =003  // controller scope get from element scope 

같은 가격이면

<div id="parent" ng-app ng-controller='...'>

$rootScope.$id = 002    // rootscope from $rootscope service
$("#parent").scope().$id =003 // controller scope get from element scope

여기서는 요소 범위에서 루트 스코프를 얻을 방법이 없지만 상관없습니다.

언급URL : https://stackoverflow.com/questions/19254400/is-it-bad-to-declare-ng-app-and-ng-controller-on-html-tag

반응형